0

I am running the below code to create a customer metabox on a page if a specific template is applied. Everything shows up fine however there is a problem. If I tick the box and save the page loads with the box checked, as expected. If I think untick it and save the page loads with the box checked again. It seems the page is always saving the custom meta info as being ticked.

Can anyone help me debug this so unticking the box works again?

Thanks

 <?php 

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);


  // check for a template type
  if ($template_file == 'template-products-listing.php') {
    function metabox_name_product_service_listing_options() {
        $screens = array( 'post', 'page' );
        foreach ( $screens as $screen ) {

            add_meta_box(
                'product_service_listing_options',
                get_option('prod_singular_name') . ' Listing Page Options',
                'callback_product_service_listing_options', 
                $screen, 
                'normal',
                'high'
            );
        }
    }
    add_action( 'add_meta_boxes', 'metabox_name_product_service_listing_options' );
  }



//Create the box and the fields on the page
function callback_product_service_listing_options( $post ) {

    wp_nonce_field( 'save_product_service_listing_options', 'nonce_product_service_listing_options' );

    //repeat for each field
    $field_value_show_rhs = get_post_meta( $post->ID, 'field_value_show_rhs', true );

    //HTML to print the fields onto the page 

    ?>
     <input type="checkbox" id="field_show_rhs" name="field_show_rhs" <?php if($field_value_show_rhs == 'yes') echo 'checked="checked"'; ?> value="yes" /><label for="field_show_rhs">Make full screen (hide right hand column)</label>
    <?php 


}





//save the form
function save_product_service_listing_options( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST['nonce_product_service_listing_options'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['nonce_product_service_listing_options'], 'save_product_service_listing_options' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }


    // Make sure that it is set.
    if ( 
        ! isset( $_POST['field_show_rhs'] ) 
        ) {
        return;
    }

    // Sanitize user input.
    $sanitized_field_show_rhs = $_POST['field_show_rhs'];

    // Update the meta field in the database.
    update_post_meta( $post_id, 'field_value_show_rhs', $sanitized_field_show_rhs );
}
add_action( 'save_post', 'save_product_service_listing_options' );

?>
  • try this change $sanitized_field_show_rhs = !isset($_POST['field_show_rhs']) ? "no" : $_POST['field_show_rhs']; – mmm Aug 6 '15 at 15:50
  • and you don't have to test current_user_can, this hook accurs after this autorisation have already been checked – mmm Aug 6 '15 at 15:51
  • oh I just have seen the "if" after "// Make sure that it is set." - it's the same as if you said "if the checkbox is not checked, then return (do nothing)" so you have to erase this "if" – mmm Aug 6 '15 at 16:52
  • Yes of course! That is only necessary for mandatory fields. Thank you :) – Kev Aug 7 '15 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.