Skip to main content
added 152 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

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); ?>

$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; }

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);
} ?>

} ?>

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);

} ?>

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);
} ?>
Source Link

Meta box checkbox won't save

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);

} ?>