Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

DA Travel Meta

class DD_Travel_Meta_Boxes {

	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(
			'travel-meta',
			__( 'Trip Details', 'dd_theme' ),
			array( $this, 'render_travel_metaboxes' ),
			'da_travel',
			'advanced',
			'high'
		);

	}

	public function render_travel_metaboxes( $post ) {

		// Retrieve an existing value from the database.
		$travel_what = get_post_meta( $post->ID, 'travel_what', true );
		$travel_start_date = get_post_meta( $post->ID, 'travel_start_date', true );
		$travel_end_date = get_post_meta( $post->ID, 'travel_end_date', true );
		$travel_included = get_post_meta( $post->ID, 'travel_included', true );
		$travel_not_included = get_post_meta( $post->ID, 'travel_not-included', true );
		$travel_cost = get_post_meta( $post->ID, 'travel_cost', true );
		$travel_documents = get_post_meta( $post->ID, 'travel_documents', true );

		// Set default values.
		if( empty( $travel_what ) ) $travel_what = '';
		if( empty( $travel_start_date ) ) $travel_start_date = '';
		if( empty( $travel_end_date ) ) $travel_end_date = '';
		if( empty( $travel_included ) ) $travel_included = '';
		if( empty( $travel_not_included ) ) $travel_not_included = '';
		if( empty( $travel_cost ) ) $travel_cost = '';
		if( empty( $travel_documents ) ) $travel_documents = '';

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

		echo '	<tr>';
		echo '		<th><label for="travel_what" class="travel_what_label">' . __( 'Trip Name', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="travel_what" name="travel_what" class="travel_what_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $travel_what ) . '">';
		echo '			<p class="description">' . __( 'Enter what the trip is.', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_start_date" class="travel_start_date_label">' . __( 'Start Date', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="travel_start_date" name="travel_start_date" class="travel_start_date_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $travel_start_date ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_end_date" class="travel_end_date_label">' . __( 'End Date', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="travel_end_date" name="travel_end_date" class="travel_end_date_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $travel_end_date ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_included" class="travel_included_label">' . __( 'What is Included', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		wp_editor( $travel_included, 'travel_included', array( 'media_buttons' => true ) );
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_not-included" class="travel_not-included_label">' . __( 'What is not included?', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		wp_editor( $travel_not_included, 'travel_not_included', array( 'media_buttons' => true ) );
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_cost" class="travel_cost_label">' . __( 'Cost', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="travel_cost" name="travel_cost" class="travel_cost_field" placeholder="' . esc_attr__( 'ex: 3199.95', 'dd_theme' ) . '" value="' . esc_attr( $travel_cost ) . '">';
		echo '			<p class="description">' . __( 'Enter the cost without the $', 'dd_theme' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="travel_documents" class="travel_documents_label">' . __( 'Documents', 'dd_theme' ) . '</label></th>';
		echo '		<td>';
		wp_editor( $travel_documents, 'travel_documents', array( 'media_buttons' => true ) );
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$travel_new_what = isset( $_POST[ 'travel_what' ] ) ? sanitize_text_field( $_POST[ 'travel_what' ] ) : '';
		$travel_new_start_date = isset( $_POST[ 'travel_start_date' ] ) ? sanitize_text_field( $_POST[ 'travel_start_date' ] ) : '';
		$travel_new_end_date = isset( $_POST[ 'travel_end_date' ] ) ? sanitize_text_field( $_POST[ 'travel_end_date' ] ) : '';
		$travel_new_included = isset( $_POST[ 'travel_included' ] ) ? wp_kses_post( $_POST[ 'travel_included' ] ) : '';
		$travel_new_not_included = isset( $_POST[ 'travel_not-included' ] ) ? wp_kses_post( $_POST[ 'travel_not-included' ] ) : '';
		$travel_new_cost = isset( $_POST[ 'travel_cost' ] ) ? sanitize_text_field( $_POST[ 'travel_cost' ] ) : '';
		$travel_new_documents = isset( $_POST[ 'travel_documents' ] ) ? wp_kses_post( $_POST[ 'travel_documents' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'travel_what', $travel_new_what );
		update_post_meta( $post_id, 'travel_start_date', $travel_new_start_date );
		update_post_meta( $post_id, 'travel_end_date', $travel_new_end_date );
		update_post_meta( $post_id, 'travel_included', $travel_new_included );
		update_post_meta( $post_id, 'travel_not-included', $travel_new_not_included );
		update_post_meta( $post_id, 'travel_cost', $travel_new_cost );
		update_post_meta( $post_id, 'travel_documents', $travel_new_documents );

	}

}

new DD_Travel_Meta_Boxes;