Meta Box Event
class Sgr_Events_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(
'events_meta',
__( 'Daten zur Veranstaltung', 'sgr' ),
array( $this, 'render_sgr_event_metabox' ),
array( 'veranstaltung', 'post' ),
'advanced',
'high'
);
}
public function render_sgr_event_metabox( $post ) {
// Retrieve an existing value from the database.
$sgr_location = get_post_meta( $post->ID, 'sgr_location', true );
$sgr_event_date = get_post_meta( $post->ID, 'sgr_event_date', true );
$sgr_video_id = get_post_meta( $post->ID, 'sgr_video_id', true );
// Set default values.
if( empty( $sgr_location ) ) $sgr_location = '';
if( empty( $sgr_event_date ) ) $sgr_event_date = '';
if( empty( $sgr_video_id ) ) $sgr_video_id = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sgr_location" class="sgr_location_label">' . __( 'Ort', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sgr_location" name="sgr_location" class="sgr_location_field" placeholder="' . esc_attr__( 'Ort', 'sgr' ) . '" value="' . esc_attr( $sgr_location ) . '">';
echo ' <p class="description">' . __( 'location of the event', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sgr_event_date" class="sgr_event_date_label">' . __( 'Datum', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="date" id="sgr_event_date" name="sgr_event_date" class="sgr_event_date_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_event_date ) . '">';
echo ' <p class="description">' . __( 'Date of the Event', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sgr_video_id" class="sgr_video_id_label">' . __( 'ID', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="number" 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">' . __( 'ID of the Video', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$sgr_new_location = isset( $_POST[ 'sgr_location' ] ) ? sanitize_text_field( $_POST[ 'sgr_location' ] ) : '';
$sgr_new_event_date = isset( $_POST[ 'sgr_event_date' ] ) ? sanitize_text_field( $_POST[ 'sgr_event_date' ] ) : '';
$sgr_new_video_id = isset( $_POST[ 'sgr_video_id' ] ) ? floatval( $_POST[ 'sgr_video_id' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sgr_location', $sgr_new_location );
update_post_meta( $post_id, 'sgr_event_date', $sgr_new_event_date );
update_post_meta( $post_id, 'sgr_video_id', $sgr_new_video_id );
}
}
new Sgr_Events_Meta_Box;