Reunion Data
class Reunion_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(
'reunions',
__( 'Reunion Data', 'dd_theme' ),
array( $this, 'render_metabox' ),
'reunion',
'advanced',
'core'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$reunion_contact = get_post_meta( $post->ID, 'reunion_contact', true );
$reunion_address = get_post_meta( $post->ID, 'reunion_address', true );
$reunion_city = get_post_meta( $post->ID, 'reunion_city', true );
$reunion_state = get_post_meta( $post->ID, 'reunion_state', true );
$reunion_zip = get_post_meta( $post->ID, 'reunion_zip', true );
$reunion_email = get_post_meta( $post->ID, 'reunion_email', true );
$reunion_website = get_post_meta( $post->ID, 'reunion_website', true );
$reunion_phone = get_post_meta( $post->ID, 'reunion_phone', true );
// Set default values.
if( empty( $reunion_contact ) ) $reunion_contact = '';
if( empty( $reunion_address ) ) $reunion_address = '';
if( empty( $reunion_city ) ) $reunion_city = '';
if( empty( $reunion_state ) ) $reunion_state = '';
if( empty( $reunion_zip ) ) $reunion_zip = '';
if( empty( $reunion_email ) ) $reunion_email = '';
if( empty( $reunion_website ) ) $reunion_website = '';
if( empty( $reunion_phone ) ) $reunion_phone = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="reunion_contact" class="reunion_contact_label">' . __( 'Contact Person', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_contact" name="reunion_contact" class="reunion_contact_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_contact ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_address" class="reunion_address_label">' . __( 'Address', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_address" name="reunion_address" class="reunion_address_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_address ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_city" class="reunion_city_label">' . __( 'City', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_city" name="reunion_city" class="reunion_city_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_city ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_state" class="reunion_state_label">' . __( 'State', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_state" name="reunion_state" class="reunion_state_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_state ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_zip" class="reunion_zip_label">' . __( 'Zip Code', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_zip" name="reunion_zip" class="reunion_zip_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_zip ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_email" class="reunion_email_label">' . __( 'E-Mail', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="email" id="reunion_email" name="reunion_email" class="reunion_email_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_email ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_website" class="reunion_website_label">' . __( 'Website Address', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="reunion_website" name="reunion_website" class="reunion_website_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_website ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="reunion_phone" class="reunion_phone_label">' . __( 'Phone Number', 'dd_theme' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="reunion_phone" name="reunion_phone" class="reunion_phone_field" placeholder="' . esc_attr__( '', 'dd_theme' ) . '" value="' . esc_attr( $reunion_phone ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$reunion_new_contact = isset( $_POST[ 'reunion_contact' ] ) ? sanitize_text_field( $_POST[ 'reunion_contact' ] ) : '';
$reunion_new_address = isset( $_POST[ 'reunion_address' ] ) ? sanitize_text_field( $_POST[ 'reunion_address' ] ) : '';
$reunion_new_city = isset( $_POST[ 'reunion_city' ] ) ? sanitize_text_field( $_POST[ 'reunion_city' ] ) : '';
$reunion_new_state = isset( $_POST[ 'reunion_state' ] ) ? sanitize_text_field( $_POST[ 'reunion_state' ] ) : '';
$reunion_new_zip = isset( $_POST[ 'reunion_zip' ] ) ? sanitize_text_field( $_POST[ 'reunion_zip' ] ) : '';
$reunion_new_email = isset( $_POST[ 'reunion_email' ] ) ? sanitize_email( $_POST[ 'reunion_email' ] ) : '';
$reunion_new_website = isset( $_POST[ 'reunion_website' ] ) ? esc_url( $_POST[ 'reunion_website' ] ) : '';
$reunion_new_phone = isset( $_POST[ 'reunion_phone' ] ) ? sanitize_text_field( $_POST[ 'reunion_phone' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'reunion_contact', $reunion_new_contact );
update_post_meta( $post_id, 'reunion_address', $reunion_new_address );
update_post_meta( $post_id, 'reunion_city', $reunion_new_city );
update_post_meta( $post_id, 'reunion_state', $reunion_new_state );
update_post_meta( $post_id, 'reunion_zip', $reunion_new_zip );
update_post_meta( $post_id, 'reunion_email', $reunion_new_email );
update_post_meta( $post_id, 'reunion_website', $reunion_new_website );
update_post_meta( $post_id, 'reunion_phone', $reunion_new_phone );
}
}
new Reunion_Meta_Box;