Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

MetaBox Video

class Video_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(
			'Video',
			__( 'Video', 'activeage' ),
			array( $this, 'video_metabox' ),
			'advice',
			'advanced',
			'default'
		);

	}

	public function video_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'video_nonce_action', 'video_nonce' );

		// Retrieve an existing value from the database.
		$video_custom_embede = get_post_meta( $post->ID, 'video_custom_embede', true );
		$video_youtube_embedding = get_post_meta( $post->ID, 'video_youtube_embedding', true );
		$video_vimeo_embedding = get_post_meta( $post->ID, 'video_vimeo_embedding', true );

		// Set default values.
		if( empty( $video_custom_embede ) ) $video_custom_embede = '';
		if( empty( $video_youtube_embedding ) ) $video_youtube_embedding = '';
		if( empty( $video_vimeo_embedding ) ) $video_vimeo_embedding = '';

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

		echo '	<tr>';
		echo '		<th><label for="video_custom_embede" class="video_custom_embede_label">' . __( 'custom embedding', 'activeage' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="video_custom_embede" name="video_custom_embede" class="video_custom_embede_field" placeholder="' . esc_attr__( 'Video URL', 'activeage' ) . '" value="' . esc_attr( $video_custom_embede ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="video_youtube_embedding" class="video_youtube_embedding_label">' . __( 'YouTube embedding', 'activeage' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="video_youtube_embedding" name="video_youtube_embedding" class="video_youtube_embedding_field" placeholder="' . esc_attr__( 'Viedo URL', 'activeage' ) . '" value="' . esc_attr( $video_youtube_embedding ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="video_vimeo_embedding" class="video_vimeo_embedding_label">' . __( 'Vimeo embedding', 'activeage' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="video_vimeo_embedding" name="video_vimeo_embedding" class="video_vimeo_embedding_field" placeholder="' . esc_attr__( 'Viedo URL', 'activeage' ) . '" value="' . esc_attr( $video_vimeo_embedding ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['video_nonce'] ) ? $_POST['video_nonce'] : '';
		$nonce_action = 'video_nonce_action';

		// Check if a nonce is set.
		if ( ! isset( $nonce_name ) )
			return;

		// Check if a nonce is valid.
		if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
			return;

		// Check if it's not an autosave.
		if ( wp_is_post_autosave( $post_id ) )
			return;

		// Sanitize user input.
		$video_new_custom_embede = isset( $_POST[ 'video_custom_embede' ] ) ? sanitize_text_field( $_POST[ 'video_custom_embede' ] ) : '';
		$video_new_youtube_embedding = isset( $_POST[ 'video_youtube_embedding' ] ) ? sanitize_text_field( $_POST[ 'video_youtube_embedding' ] ) : '';
		$video_new_vimeo_embedding = isset( $_POST[ 'video_vimeo_embedding' ] ) ? sanitize_text_field( $_POST[ 'video_vimeo_embedding' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'video_custom_embede', $video_new_custom_embede );
		update_post_meta( $post_id, 'video_youtube_embedding', $video_new_youtube_embedding );
		update_post_meta( $post_id, 'video_vimeo_embedding', $video_new_vimeo_embedding );

	}

}

new Video_Meta_Box;