0

I have several fields mostly text inputs to save additional info to the posts, all the text boxes save but i cant't get the checkbox to save the value!

Wondered whether any of you geniuses out there could help me. Thanks in advance.

The checkbox is below, which I echo the ck_sale in the checkbox input

$ck_sale = get_post_meta($post->ID, 'ck_sale_item', true); ?>

<?php //Save the information in the metabox
add_action( 'save_post', 'mb_product_details_save');
function mb_product_details_save( $post_id ) {
     // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;  
    }
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['product_details_nonce'] ) || !wp_verify_nonce( $_POST['product_details_nonce'], 'mb_product_details_nonce' ) ) {
        return;    
    }
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) {
        return;
    }

    //Set the data in the fields before saving
    if( isset( $_POST['tb_RRP_price'] ) ) {
        update_post_meta( $post_id, 'tb_RRP_price', wp_kses($_POST['tb_RRP_price']));
    }
     if( isset( $_POST['tb_trade_price'] ) ) {
        update_post_meta( $post_id, 'tb_trade_price', wp_kses($_POST['tb_trade_price']));
    }
     if( isset( $_POST['tb_colours'] ) ) {
        update_post_meta( $post_id, 'tb_colours', wp_kses($_POST['tb_colours']));
    }
     if( isset( $_POST['tb_quantity'] ) ) {
        update_post_meta( $post_id, 'tb_quantity', wp_kses($_POST['tb_quantity']));
    }

    if($_POST["ck_sale_item"] == "on") {
        $featured = "on";
    }
    else {
        $featured = "off";
    }
    update_post_meta($post->ID, 'ck_sale_item', $featured);
} ?>
  • Welcome to WPSE. Please make proper use of the WYSIWYG editor when writing posts. I have already corrected your code format to make it readable. Please keep this in mind:-) – Pieter Goosen Dec 20 '14 at 19:20
  • Yes thanks, I tried to format it but only half would! thanks for editing it – Bryony Lloyd Dec 20 '14 at 19:23
  • How are you displaying the post meta / how do you know it's not saving? – Howdy_McGee Dec 20 '14 at 19:42
0

I found the answer now it was the way i was trying to save it this is what I done in the save function

    if( isset( $_POST[ 'ck_sale_item' ] ) ) {
    update_post_meta( $post_id, 'ck_sale_item', 'on' );
} else {
    update_post_meta( $post_id, 'ck_sale_item', 'off' );
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.