0

am using this code to create a Metabox for CPT. Now I am able to view the updateded / saved Text input on post but the Checkbox and selected options return null after save or update. Can you please take a look the code and let me know what I am doing wrong here?

 add_action('add_meta_boxes', 'my_cmbox_add');
 add_action('save_post', 'save_options');
function my_cmbox_add()
{
$post_types = array ( 'movie', 'book', 'web' );
 foreach( $post_types as $post_type ){
    add_meta_box(
        "product-detail",
        "Product Details ",
        "render_mtbox",
        $post_type,
        "normal",
        "low"
    );
    }
}
function render_mtbox()
{
    global $post;
    $metaValues = get_post_custom($post->ID);
    $offer      = isset( $metaValues['offer'] ) ? esc_attr( $metaValues['offer'][0] ) : ”;
    $priceVal   = isset( $metaValues['priceValue'] ) ? esc_attr( $metaValues['priceValue'][0] ) : ”;
    $discount   = isset( $metaValues['dicounts'] ) ? esc_attr( $metaValues['dicounts'][0] ) : ”;
?>

<table style="width:70%">
    <tr>
    <td style="width:15%"><label for="offer">Special Offer</label></td> 
    <td style="width:10%"><input type="checkbox" id="offer" name="offer" <?php checked( $check, 'on' ); ?> /></td>
    <td align="right"><label for="prodprice">Product Price</label></td>
    <td align="left"><input type="text" name="priceValue" id="prodprice" value="<?php echo $priceVal; ?>" /></td>
    <td align="right"> <label for="dicounts-list">Discount</label></td> 
    <td align="left"> 
        <select name="dicounts" id="dicounts-list">
            <option value="five" <?php selected( $selected, 'five' ); ?>>% 5</option>
            <option value="ten" <?php selected( $selected, 'ten' ); ?>>% 10</option>
            <option value="fifteen" <?php selected( $selected, 'fifteen' ); ?>>% 15</option>
            <option value="twentyfive" <?php selected( $selected, 'twenty' ); ?>>% 20</option>
        </select>
    </td>
    </tr>
    </table>
<?php
}
function save_options()
{
 $post_types =  array ( 'movie', 'book', 'web' );
   global $post;
   $cpt = $post->post_type;

    if ((!isset($_POST['offer'])) && (!in_array( $cpt, $post_types )))
    {
        return $post;
    }
    update_post_meta($post->ID, "priceValue", $_POST['priceValue']);
    update_post_meta($post->ID, "offer", $_POST['offer']);
    update_post_meta($post->ID, "dicounts", $_POST['dicounts']);
}

Update

I also tried this way

$my_data = $_POST['offer'] ? true : false;
update_post_meta( $post->ID,  'offer', $my_data );

but still not getting checked checkbox after save

Thanks

  • Your code uses $checked and $selected but neither seem to be defined. – s_ha_dum Oct 21 '14 at 17:55
  • Hi s_ha_dum, thanks for reply but can you please let me know where and how should I define them? – Behseini Oct 21 '14 at 18:08
  • But I am using the WordPress checked() function . is nit enough? – Behseini Oct 21 '14 at 18:19
  • I also updated the code as : <td style="width:10%"><input type="checkbox" id="offer" name="offer" <?php checked( $offer, 'on' ); ?> /></td> but still not working! – Behseini Oct 21 '14 at 18:24

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.