Meta Box Videos
class Sgr_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(
'sgr_video_meta',
__( 'Videos', 'sgr' ),
array( $this, 'sgr_render_video_metabox' ),
'veranstaltung',
'side',
'default'
);
}
public function sgr_render_video_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'sgr_nonce_action', 'sgr_nonce' );
// Retrieve an existing value from the database.
$sgr_video_id = get_post_meta( $post->ID, 'sgr_video_id', true );
$sgr_plan_video_1 = get_post_meta( $post->ID, 'sgr_plan_video_1', true );
$sgr_plan_video_2 = get_post_meta( $post->ID, 'sgr_plan_video_2', true );
// Set default values.
if( empty( $sgr_video_id ) ) $sgr_video_id = '';
if( empty( $sgr_plan_video_1 ) ) $sgr_plan_video_1 = '';
if( empty( $sgr_plan_video_2 ) ) $sgr_plan_video_2 = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sgr_video_id" class="sgr_video_id_label">' . __( 'Projekt', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sgr_video_id" name="sgr_video_id" class="sgr_video_id_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_video_id ) . '">';
echo ' <p class="description">' . __( 'Projekt-ID', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sgr_plan_video_1" class="sgr_plan_video_1_label">' . __( 'Teil 1', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sgr_plan_video_1" name="sgr_plan_video_1" class="sgr_plan_video_1_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_plan_video_1 ) . '">';
echo ' <p class="description">' . __( 'ID der Tabelle', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sgr_plan_video_2" class="sgr_plan_video_2_label">' . __( 'Teil 2', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sgr_plan_video_2" name="sgr_plan_video_2" class="sgr_plan_video_2_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_plan_video_2 ) . '">';
echo ' <p class="description">' . __( 'ID der Tabelle', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['sgr_nonce'] ) ? $_POST['sgr_nonce'] : '';
$nonce_action = 'sgr_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.
$sgr_new_video_id = isset( $_POST[ 'sgr_video_id' ] ) ? sanitize_text_field( $_POST[ 'sgr_video_id' ] ) : '';
$sgr_new_plan_video_1 = isset( $_POST[ 'sgr_plan_video_1' ] ) ? sanitize_text_field( $_POST[ 'sgr_plan_video_1' ] ) : '';
$sgr_new_plan_video_2 = isset( $_POST[ 'sgr_plan_video_2' ] ) ? sanitize_text_field( $_POST[ 'sgr_plan_video_2' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sgr_video_id', $sgr_new_video_id );
update_post_meta( $post_id, 'sgr_plan_video_1', $sgr_new_plan_video_1 );
update_post_meta( $post_id, 'sgr_plan_video_2', $sgr_new_plan_video_2 );
}
}
new Sgr_Video_Meta_Box;