The following code creates a metabox and Yes/No Select form field in the WordPress admin area.
Please can someone help me update my code so it uses a simple Checkbox rather than a Select field? Please include validation and sanitization checks to ensure the submitted data is valid and safe.
Thank you
function myprefix_menu_metabox() {
add_meta_box( 'myprefix_menu_metabox', __( 'Menu Item' ), 'myprefix_menu_metabox_callback', 'page', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'myprefix_menu_metabox' );
function myprefix_menu_metabox_callback( $post ) {
wp_nonce_field( 'menu_metabox_check', 'menu_metabox_nonce' );
$value = get_post_meta( $post->ID, '_myprefix_include_in_menu', true );
$selected = isset( $value ) ? esc_attr( $value ) : '';
?>
<p>
<select name="include_in_menu">
<option value="1" <?php selected( $selected, '1' ); ?>><?php _e( 'Yes' )?></option>
<option value="0" <?php selected( $selected, '0' ); ?>><?php _e( 'No' )?></option>
</select>
</p><?php
}
function myprefix_save_menu_metabox( $post_id ) {
if ( ! isset( $_POST[ 'menu_metabox_nonce' ] ) ) { return; }
if ( ! wp_verify_nonce( $_POST[ 'menu_metabox_nonce' ], 'menu_metabox_check' ) ) { return; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
if ( ! current_user_can( 'edit_page', $post_id ) ) { return; }
if ( isset( $_POST[ 'include_in_menu' ] ) ) {
$value = $_POST[ 'include_in_menu' ];
if ( in_array( $value, array( '0', '1' ), true ) ) {
update_post_meta( $post_id, '_myprefix_include_in_menu', $value );
}
}
}
add_action( 'save_post', 'myprefix_save_menu_metabox' );
timDesain Nanang answers:
try the following code:
function wpq_priorities(){
return array(
9 => 'Lorem Ipsum',
7 => 'Dolor',
5 => 'Sit Amet',
);
}
add_action('add_meta_boxes', 'wpq_meta_add_new');
function wpq_meta_add_new() {
add_meta_box('wpq_meta_priority', 'Priority', 'wpq_meta_priority_form', 'post', 'side', 'high' );
}
function wpq_meta_priority_form($post) {
wp_nonce_field('wpq_meta_priority_form', 'wpq_meta_priority_form_nonce');
$current = maybe_unserialize(get_post_meta($post->ID, 'wpq_cf_priority', true));
foreach(wpq_priorities() as $key=>$value){
echo '<p><label for="cb-'.$key.'"><input type="checkbox" name="wpq_cf_priority[]" id="cb-'.$key.'" value="'.$key.'" '.(in_array($key, $current)?' checked="checked"':'').' /> '.($value).' </label></p>';
}
}
add_action('save_post', 'wpq_meta_priority_action');
function wpq_meta_priority_action($post_id) {
if(!isset($_POST['wpq_meta_priority_form_nonce'])) return $post_id;
if(!wp_verify_nonce($_POST['wpq_meta_priority_form_nonce'], 'wpq_meta_priority_form')) return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
update_post_meta($post_id, 'wpq_cf_priority', maybe_serialize($_POST['wpq_cf_priority']));
}
designbuildtest comments:
Many thanks timDesain.
I will only ever have one checkbox however. The Checkbox simply needs to indicate on or off - like I currently do with my YES/NO Select field. Sorry for the confusion.
Is it possible to update and simplify your code to reflect just a single checkbox?
Much appreciated.
timDesain Nanang comments:
sure, wait a minute
timDesain Nanang comments:
you should use radio input instead for Yes/No
function wpq_priorities(){
return array(
1 => 'Yes',
0 => 'No',
);
}
add_action('add_meta_boxes', 'wpq_meta_add_new');
function wpq_meta_add_new() {
add_meta_box('wpq_meta_onoff', 'onoff', 'wpq_meta_onoff_form', 'post', 'side', 'high' );
}
function wpq_meta_onoff_form($post) {
wp_nonce_field('wpq_meta_onoff_form', 'wpq_meta_onoff_form_nonce');
$current = (get_post_meta($post->ID, 'wpq_cf_onoff', true));
//if No as default
//$current = intval(get_post_meta($post->ID, 'wpq_cf_onoff', true));
foreach(wpq_priorities() as $key=>$value){
echo '<p><label for="cb-'.$key.'"><input type="radio" name="wpq_cf_onoff" id="cb-'.$key.'" value="'.$key.'" '.checked($key, $current, false).' /> '.($value).' </label></p>';
}
}
add_action('save_post', 'wpq_meta_onoff_action');
function wpq_meta_onoff_action($post_id) {
if(!isset($_POST['wpq_meta_onoff_form_nonce'])) return $post_id;
if(!wp_verify_nonce($_POST['wpq_meta_onoff_form_nonce'], 'wpq_meta_onoff_form')) return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
update_post_meta($post_id, 'wpq_cf_onoff', esc_attr($_POST['wpq_cf_onoff']));
//if No as default
//update_post_meta($post_id, 'wpq_cf_onoff', intval($_POST['wpq_cf_onoff']));
}
designbuildtest comments:
Sorry, I need a Checkbox, not Radio Buttons. Similar to how JetPack's ShareDaddy module works, the page feature I'm building will either be present (value = 1) or not present (no value)
Thanks.
timDesain Nanang comments:
i see, here is the final code:
add_action('add_meta_boxes', 'wpq_meta_add_new');
function wpq_meta_add_new() {
add_meta_box('wpq_meta_onoff', 'Agreement', 'wpq_meta_onoff_form', 'post', 'side', 'high' );
}
function wpq_meta_onoff_form($post) {
wp_nonce_field('wpq_meta_onoff_form', 'wpq_meta_onoff_form_nonce');
$current = intval(get_post_meta($post->ID, 'wpq_cf_onoff', true));
echo '<p><label for="yes"><input type="checkbox" name="wpq_cf_onoff" id="yes" value="1" '.checked(1, $current, false).' /> '.($value).' I Agree with TOS</label></p>';
}
add_action('save_post', 'wpq_meta_onoff_action');
function wpq_meta_onoff_action($post_id) {
if(!isset($_POST['wpq_meta_onoff_form_nonce'])) return $post_id;
if(!wp_verify_nonce($_POST['wpq_meta_onoff_form_nonce'], 'wpq_meta_onoff_form')) return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$onoff = intval($_POST['wpq_cf_onoff']);
update_post_meta($post_id, 'wpq_cf_onoff', ($onoff==1 ? 1 : ''));
}