Video Slider Metabox
class Video_Slider {
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(
'vslider',
__( 'Video Slider Information', 'batachcreativeco_19' ),
array( $this, 'render_metabox' ),
'vslider',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$ = get_post_meta( $post->ID, '', true );
$ = get_post_meta( $post->ID, '', true );
$ = get_post_meta( $post->ID, '', true );
// Set default values.
if( empty( $ ) ) $ = '';
if( empty( $ ) ) $ = '';
if( empty( $ ) ) $ = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="" class="_label">' . __( 'Video Link', 'batachcreativeco_19' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="" name="" class="_field" placeholder="' . esc_attr__( '', 'batachcreativeco_19' ) . '" value="' . esc_attr( $ ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="" class="_label">' . __( 'Video Type', 'batachcreativeco_19' ) . '</label></th>';
echo ' <td>';
echo ' <select id="" name="" class="_field">';
echo ' <option value="YouTube" ' . selected( $, 'YouTube', false ) . '> ' . __( '', 'batachcreativeco_19' ) . '</option>';
echo ' <option value="Vimeo" ' . selected( $, 'Vimeo', false ) . '> ' . __( '', 'batachcreativeco_19' ) . '</option>';
echo ' <option value="MP4" ' . selected( $, 'MP4', false ) . '> ' . __( '', 'batachcreativeco_19' ) . '</option>';
echo ' </select>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="" class="_label">' . __( 'Video Author', 'batachcreativeco_19' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="" name="" class="_field" placeholder="' . esc_attr__( '', 'batachcreativeco_19' ) . '" value="' . esc_attr( $ ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$nonce_action = '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 the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$new_ = isset( $_POST[ '' ] ) ? esc_url( $_POST[ '' ] ) : '';
$new_ = isset( $_POST[ '' ] ) ? $_POST[ '' ] : '';
$new_ = isset( $_POST[ '' ] ) ? sanitize_text_field( $_POST[ '' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, '', $new_ );
update_post_meta( $post_id, '', $new_ );
update_post_meta( $post_id, '', $new_ );
}
}
new Video_Slider;