mb_event
class mb_program {
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(
'program_details',
__( 'Program', 'text_domain' ),
array( $this, 'render_metabox' ),
'pt_program',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$program_sec_title = get_post_meta( $post->ID, 'program_sec-title', true );
$program_date = get_post_meta( $post->ID, 'program_date', true );
$program_time = get_post_meta( $post->ID, 'program_time', true );
$program_price = get_post_meta( $post->ID, 'program_price', true );
// Set default values.
if( empty( $program_sec_title ) ) $program_sec_title = '';
if( empty( $program_date ) ) $program_date = '';
if( empty( $program_time ) ) $program_time = '';
if( empty( $program_price ) ) $program_price = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="program_sec-title" class="program_sec-title_label">' . __( 'Subtitle', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="program_sec_title" name="program_sec-title" class="program_sec_title_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $program_sec_title ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="program_date" class="program_date_label">' . __( 'Date', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="date" id="program_date" name="program_date" class="program_date_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $program_date ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="program_time" class="program_time_label">' . __( 'Time', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="time" id="program_time" name="program_time" class="program_time_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $program_time ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="program_price" class="program_price_label">' . __( 'Price', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="number" id="program_price" name="program_price" class="program_price_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $program_price ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Sanitize user input.
$program_new_sec_title = isset( $_POST[ 'program_sec-title' ] ) ? sanitize_text_field( $_POST[ 'program_sec-title' ] ) : '';
$program_new_date = isset( $_POST[ 'program_date' ] ) ? sanitize_text_field( $_POST[ 'program_date' ] ) : '';
$program_new_time = isset( $_POST[ 'program_time' ] ) ? sanitize_text_field( $_POST[ 'program_time' ] ) : '';
$program_new_price = isset( $_POST[ 'program_price' ] ) ? floatval( $_POST[ 'program_price' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'program_sec-title', $program_new_sec_title );
update_post_meta( $post_id, 'program_date', $program_new_date );
update_post_meta( $post_id, 'program_time', $program_new_time );
update_post_meta( $post_id, 'program_price', $program_new_price );
}
}
new mb_program;