MVF meta
class mvf {
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(
'mvf_video_fields',
__( 'Video fields', 'mvf_video' ),
array( $this, 'render_metabox_mvf_video' ),
'videos',
'advanced',
'core'
);
}
public function render_metabox_mvf_video( $post ) {
// Retrieve an existing value from the database.
$mvfmvf_video_subtitle = get_post_meta( $post->ID, 'mvfmvf_video_subtitle', true );
$mvfmvf_video_description = get_post_meta( $post->ID, 'mvfmvf_video_description', true );
$mvfmvf_video_type = get_post_meta( $post->ID, 'mvfmvf_video_type', true );
$mvfmvf_video_id = get_post_meta( $post->ID, 'mvfmvf_video_id', true );
// Set default values.
if( empty( $mvfmvf_video_subtitle ) ) $mvfmvf_video_subtitle = '';
if( empty( $mvfmvf_video_description ) ) $mvfmvf_video_description = '';
if( empty( $mvfmvf_video_type ) ) $mvfmvf_video_type = '';
if( empty( $mvfmvf_video_id ) ) $mvfmvf_video_id = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="mvfmvf_video_subtitle" class="mvfmvf_video_subtitle_label">' . __( 'Subtitle', 'mvf_video' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="mvfmvf_video_subtitle" name="mvfmvf_video_subtitle" class="mvfmvf_video_subtitle_field" placeholder="' . esc_attr__( '', 'mvf_video' ) . '">' . $mvfmvf_video_subtitle . '</textarea>';
echo ' <p class="description">' . __( 'subtitle text to describe the video', 'mvf_video' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="mvfmvf_video_description" class="mvfmvf_video_description_label">' . __( 'Description', 'mvf_video' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="mvfmvf_video_description" name="mvfmvf_video_description" class="mvfmvf_video_description_field" placeholder="' . esc_attr__( '', 'mvf_video' ) . '">' . $mvfmvf_video_description . '</textarea>';
echo ' <p class="description">' . __( 'a brief description of the video', 'mvf_video' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="mvfmvf_video_type" class="mvfmvf_video_type_label">' . __( 'Video Source', 'mvf_video' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="radio" name="mvfmvf_video_type" class="mvfmvf_video_type_field" value="mvfyoutube" ' . checked( $mvfmvf_video_type, 'mvfyoutube', false ) . '> ' . __( 'Youtube', 'mvf_video' ) . '</label><br>';
echo ' <label><input type="radio" name="mvfmvf_video_type" class="mvfmvf_video_type_field" value="mvfvimeo" ' . checked( $mvfmvf_video_type, 'mvfvimeo', false ) . '> ' . __( 'Vimeo', 'mvf_video' ) . '</label><br>';
echo ' <label><input type="radio" name="mvfmvf_video_type" class="mvfmvf_video_type_field" value="mvfdailymotion" ' . checked( $mvfmvf_video_type, 'mvfdailymotion', false ) . '> ' . __( 'Dailymotion', 'mvf_video' ) . '</label><br>';
echo ' <p class="description">' . __( 'Please select the video source', 'mvf_video' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="mvfmvf_video_id" class="mvfmvf_video_id_label">' . __( 'Video ID', 'mvf_video' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="mvfmvf_video_id" name="mvfmvf_video_id" class="mvfmvf_video_id_field" placeholder="' . esc_attr__( '', 'mvf_video' ) . '" value="' . esc_attr( $mvfmvf_video_id ) . '">';
echo ' <p class="description">' . __( 'The ID for the video', 'mvf_video' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$mvfnew_mvf_video_subtitle = isset( $_POST[ 'mvfmvf_video_subtitle' ] ) ? sanitize_text_field( $_POST[ 'mvfmvf_video_subtitle' ] ) : '';
$mvfnew_mvf_video_description = isset( $_POST[ 'mvfmvf_video_description' ] ) ? sanitize_text_field( $_POST[ 'mvfmvf_video_description' ] ) : '';
$mvfnew_mvf_video_type = isset( $_POST[ 'mvfmvf_video_type' ] ) ? $_POST[ 'mvfmvf_video_type' ] : '';
$mvfnew_mvf_video_id = isset( $_POST[ 'mvfmvf_video_id' ] ) ? sanitize_text_field( $_POST[ 'mvfmvf_video_id' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'mvfmvf_video_subtitle', $mvfnew_mvf_video_subtitle );
update_post_meta( $post_id, 'mvfmvf_video_description', $mvfnew_mvf_video_description );
update_post_meta( $post_id, 'mvfmvf_video_type', $mvfnew_mvf_video_type );
update_post_meta( $post_id, 'mvfmvf_video_id', $mvfnew_mvf_video_id );
}
}
new mvf;