RWB Showdates
class Showdate_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(
'showdatesmeta',
__( 'Show Dates Meta', 'text_domain' ),
array( $this, 'render_metabox' ),
'show_dates',
'normal',
'high'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$showvenue = get_post_meta( $post->ID, 'showvenue', true );
$showplace = get_post_meta( $post->ID, 'showplace', true );
$showurl = get_post_meta( $post->ID, 'showurl', true );
// Set default values.
if( empty( $showvenue ) ) $showvenue = '';
if( empty( $showplace ) ) $showplace = '';
if( empty( $showurl ) ) $showurl = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="showvenue" class="showvenue_label">' . __( 'Venue', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="showvenue" name="showvenue" class="showvenue_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $showvenue ) . '">';
echo ' <p class="description">' . __( 'ex. Burdock', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="showplace" class="showplace_label">' . __( 'City, Country', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="showplace" name="showplace" class="showplace_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $showplace ) . '">';
echo ' <p class="description">' . __( 'Berlin, Germany', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="showurl" class="showurl_label">' . __( 'Event URL', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="showurl" name="showurl" class="showurl_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $showurl ) . '">';
echo ' <p class="description">' . __( 'Ticket or Facebook Event Link', 'text_domain' ) . '</p>';
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 it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$new_showvenue = isset( $_POST[ 'showvenue' ] ) ? sanitize_text_field( $_POST[ 'showvenue' ] ) : '';
$new_showplace = isset( $_POST[ 'showplace' ] ) ? sanitize_text_field( $_POST[ 'showplace' ] ) : '';
$new_showurl = isset( $_POST[ 'showurl' ] ) ? sanitize_text_field( $_POST[ 'showurl' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'showvenue', $new_showvenue );
update_post_meta( $post_id, 'showplace', $new_showplace );
update_post_meta( $post_id, 'showurl', $new_showurl );
}
}
new Showdate_Meta_box;