Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Wreck Site Meta

class Custom_Meta_Box {

	public function __construct() {

		if ( is_admin() ) {
			add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
			add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
		}

	}

	public function init_metabox() {

		add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
		add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );

	}

	public function add_metabox() {

		add_meta_box(
			'divesite-meta',
			__( 'Wreck Site Details', 'dd_theme' ),
			array( $this, 'render_ds_metabox' ),
			'divesite',
			'advanced',
			'high'
		);

	}

	public function render_ds_metabox( $post ) {

		// Retrieve an existing value from the database.
		$wreck_depth = get_post_meta( $post->ID, 'wreck_depth', true );
		$wreck_sunk = get_post_meta( $post->ID, 'wreck_sunk', true );
		$wreck_cost = get_post_meta( $post->ID, 'wreck_cost', true );
		$wreck_maplink = get_post_meta( $post->ID, 'wreck_maplink', true );

		// Set default values.
		if( empty( $wreck_depth ) ) $wreck_depth = '';
		if( empty( $wreck_sunk ) ) $wreck_sunk = '';
		if( empty( $wreck_cost ) ) $wreck_cost = '';
		if( empty( $wreck_maplink ) ) $wreck_maplink = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="wreck_depth" class="wreck_depth_label">' . __( 'Depth / Depth Range', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="wreck_depth" name="wreck_depth" class="wreck_depth_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $wreck_depth ) . '">';
		echo '			<p class="description">' . __( 'Include Feet', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="wreck_sunk" class="wreck_sunk_label">' . __( 'Sinking Date', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="wreck_sunk" name="wreck_sunk" class="wreck_sunk_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $wreck_sunk ) . '">';
		echo '			<p class="description">' . __( 'Enter the date or approximate time of sinking.', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="wreck_cost" class="wreck_cost_label">' . __( 'Cost', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="wreck_cost" name="wreck_cost" class="wreck_cost_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $wreck_cost ) . '">';
		echo '			<p class="description">' . __( 'Enter the cost and if it&rsquo;s a one tank or two.', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="wreck_maplink" class="wreck_maplink_label">' . __( 'Map Link', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="wreck_maplink" name="wreck_maplink" class="wreck_maplink_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $wreck_maplink ) . '">';
		echo '			<p class="description">' . __( 'Saved for Future use maybe', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$wreck_new_depth = isset( $_POST[ 'wreck_depth' ] ) ? sanitize_text_field( $_POST[ 'wreck_depth' ] ) : '';
		$wreck_new_sunk = isset( $_POST[ 'wreck_sunk' ] ) ? sanitize_text_field( $_POST[ 'wreck_sunk' ] ) : '';
		$wreck_new_cost = isset( $_POST[ 'wreck_cost' ] ) ? sanitize_text_field( $_POST[ 'wreck_cost' ] ) : '';
		$wreck_new_maplink = isset( $_POST[ 'wreck_maplink' ] ) ? sanitize_text_field( $_POST[ 'wreck_maplink' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'wreck_depth', $wreck_new_depth );
		update_post_meta( $post_id, 'wreck_sunk', $wreck_new_sunk );
		update_post_meta( $post_id, 'wreck_cost', $wreck_new_cost );
		update_post_meta( $post_id, 'wreck_maplink', $wreck_new_maplink );

	}

}

new Custom_Meta_Box;