fivevines
class Custom_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(
'therapistinfo',
__( 'Bio & Info', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$custom_infocreds = get_post_meta( $post->ID, 'custom_infocreds', true );
$custom_infobio = get_post_meta( $post->ID, 'custom_infobio', true );
$custom_infolinks = get_post_meta( $post->ID, 'custom_infolinks', true );
$custom_infoemail = get_post_meta( $post->ID, 'custom_infoemail', true );
$custom_infoareas = get_post_meta( $post->ID, 'custom_infoareas', true );
// Set default values.
if( empty( $custom_infocreds ) ) $custom_infocreds = '';
if( empty( $custom_infobio ) ) $custom_infobio = '';
if( empty( $custom_infolinks ) ) $custom_infolinks = '';
if( empty( $custom_infoemail ) ) $custom_infoemail = '';
if( empty( $custom_infoareas ) ) $custom_infoareas = 'Winnipeg';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_infocreds" class="custom_infocreds_label">' . __( 'Credentials & Certifications', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_infocreds" name="custom_infocreds" class="custom_infocreds_field" placeholder="' . esc_attr__( 'Certs, Creds', 'text_domain' ) . '" value="' . esc_attr( $custom_infocreds ) . '">';
echo ' <p class="description">' . __( 'Full', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_infobio" class="custom_infobio_label">' . __( 'Short Bio', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="custom_infobio" name="custom_infobio" class="custom_infobio_field" placeholder="' . esc_attr__( 'John Does is a therapist. They studied...', 'text_domain' ) . '">' . $custom_infobio . '</textarea>';
echo ' <p class="description">' . __( 'General Information', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_infolinks" class="custom_infolinks_label">' . __( 'Social Links', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_infolinks" name="custom_infolinks" class="custom_infolinks_field" placeholder="' . esc_attr__( '["https://instagram.com/therapist", ...]', 'text_domain' ) . '" value="' . esc_attr( $custom_infolinks ) . '">';
echo ' <p class="description">' . __( 'In this format and order only (include [ ] and " " symbols): ["facebook link","linkedin link", "twitter link"]', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_infoemail" class="custom_infoemail_label">' . __( 'email', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="email" id="custom_infoemail" name="custom_infoemail" class="custom_infoemail_field" placeholder="' . esc_attr__( '[email protected]', 'text_domain' ) . '" value="' . esc_attr( $custom_infoemail ) . '">';
echo ' <p class="description">' . __( 'email', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_infoareas" class="custom_infoareas_label">' . __( 'Preferred Practice Areas Or Specialties', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_infoareas" name="custom_infoareas" class="custom_infoareas_field" placeholder="' . esc_attr__( 'Downtown Location', 'text_domain' ) . '" value="' . esc_attr( $custom_infoareas ) . '">';
echo ' <p class="description">' . __( 'Areas', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$custom_new_infocreds = isset( $_POST[ 'custom_infocreds' ] ) ? sanitize_text_field( $_POST[ 'custom_infocreds' ] ) : '';
$custom_new_infobio = isset( $_POST[ 'custom_infobio' ] ) ? sanitize_text_field( $_POST[ 'custom_infobio' ] ) : '';
$custom_new_infolinks = isset( $_POST[ 'custom_infolinks' ] ) ? sanitize_text_field( $_POST[ 'custom_infolinks' ] ) : '';
$custom_new_infoemail = isset( $_POST[ 'custom_infoemail' ] ) ? sanitize_email( $_POST[ 'custom_infoemail' ] ) : '';
$custom_new_infoareas = isset( $_POST[ 'custom_infoareas' ] ) ? sanitize_text_field( $_POST[ 'custom_infoareas' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_infocreds', $custom_new_infocreds );
update_post_meta( $post_id, 'custom_infobio', $custom_new_infobio );
update_post_meta( $post_id, 'custom_infolinks', $custom_new_infolinks );
update_post_meta( $post_id, 'custom_infoemail', $custom_new_infoemail );
update_post_meta( $post_id, 'custom_infoareas', $custom_new_infoareas );
}
}
new Custom_Meta_Box;