Skip to content

Instantly share code, notes, and snippets.

@ibrahimkholil
Created December 10, 2018 07:08
Show Gist options
  • Save ibrahimkholil/0541477a0a5424290043c38151d8e825 to your computer and use it in GitHub Desktop.
Save ibrahimkholil/0541477a0a5424290043c38151d8e825 to your computer and use it in GitHub Desktop.
Meta box
<?php
/* Doctor META BOXES */
add_action( 'add_meta_boxes', 'brb_doctor_add_meta_box' );
add_action( 'save_post', 'brb_save_doctor_position_data' );
function brb_doctor_add_meta_box() {
add_meta_box( 'doctor_position', 'Doctor Position', 'brb_doctort_position_callback', 'doctor' );
}
function brb_doctort_position_callback( $post ) {
wp_nonce_field( 'brb_save_doctor_position_data', 'brb_doctort_position_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_doctort_position_value_key', true );
$depselect = get_post_meta( $post->ID, '_doctort_department_value_key', true );
echo '<label for="brb_doctor_position_field">Doctor Position: </lable>';
echo '<input type="text" id="brb_doctor_position_field" name="brb_doctor_position_field" value="' . esc_attr( $value ) . '" size="50" />';
?>
<p><label for="select_doctor_department">Select Department: </label>
<select name='select_doctor_department' id='select_doctor_department'>
<?php
global $wpdb;
$custom_post_type = 'department'; // define your custom post type slug her
// A sql query to return all post titles
$results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );
?>
<option value="" <?php if ( empty($results) ) echo ' selected'; ?>>None/Not Applicable</option>
<?php foreach ($results as $index => $post):
//var_dump( $post['ID']);
?>
<option value="<?php echo esc_attr($post['ID']); ?>" <?php selected( $depselect, $post['ID'] ); ?>><?php echo esc_html($post['post_title']); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php }
function brb_save_doctor_position_data( $post_id ) {
if( ! isset( $_POST['brb_doctort_position_meta_box_nonce'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST['brb_doctort_position_meta_box_nonce'], 'brb_save_doctor_position_data') ) {
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if( ! isset( $_POST['brb_doctor_position_field'] ) ) {
return;
}
if( ! isset( $_POST['select_doctor_department'] ) ) {
return;
}
$my_data = sanitize_text_field( $_POST['brb_doctor_position_field'] );
$select_department = sanitize_text_field( $_POST['select_doctor_department'] );
update_post_meta( $post_id, '_doctort_position_value_key', $my_data );
update_post_meta( $post_id, '_doctort_department_value_key', $select_department );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment