logo

$15
Modifying function for checkboxes

Hello!
Need a little modification to this function. Right now, it's for a custom post type. Someone types a value in a text box and saves it. Then it can be retrieved.

However, instead of a text box, I need to have a few checkboxes that save whether or not they are checked. Then I need a way to retreive that value: "if box 1 is checked, display this:" "if box 2 is checked, display this:".


<?php
add_action("admin_init", "admin_init");
add_action('save_post', 'save_price');

function admin_init(){
add_meta_box("prodInfo-meta", "Product Options", "meta_options", "product", "side", "low");
}

function meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$price = $custom["price"][0];
?>
<label>Price:</label><input name="price" value="<?php echo $price; ?>" />
<?php
}

function save_price(){
global $post;
update_post_meta($post->ID, "price", $_POST["price"]);
}
?>

Mike McAlister | 06/26/10 at 4:01am | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    06/26/10
    4:38am
    Oleg Butuzov says:

    meta box...


    function meta_box($post){
    if ($post->ID >= 1) {
    $_checkbox_1 = get_post_meta($post->ID, '_checkbox_1', true);
    $_checkbox_2 = get_post_meta($post->ID, '_checkbox_2', true);
    }
    ?>
    <table width="100%">
    <tr>
    <th style="padding:5px; "><label for="_checkbox_1" style="text-align:left;">Option 1</label></th>
    <td style="padding:5px;">
    <input type="checkbox" id="_checkbox_1" name="_checkbox_1" value="<?php echo (isset($_checkbox_1) && $_checkbox_1 != '' ? 'checked' : ''); ?>" />
    </td>
    </tr>
    <tr>
    <th style="padding:5px; "><label for="_checkbox_2" style="text-align:left;">Option 2</label></th>
    <td style="padding:5px;">
    <input type="checkbox" id="_checkbox_2" name="_checkbox_2" value="<?php echo (isset($_checkbox_2) && $_checkbox_2 != '' ? 'checked' : ''); ?>" />
    </td>
    </tr>
    </table>
    <?
    }



    save post handler

    function save_postmeta($id){
    global $wpdb;
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {

    if (isset($_POST['_checkbox_1']) && $_POST['_checkbox_1'] == 'on'){
    delete_post_meta($id, '_checkbox_2');
    if (!update_post_meta($id, '_checkbox_1', 1)){
    add_post_meta($id, '_checkbox_1', 1, true);
    }
    } else {
    delete_post_meta($id, '_checkbox_1');
    if (!update_post_meta($id, '_checkbox_2', 1)){
    add_post_meta($id, '_checkbox_2', 1, true);
    }
    }

    }
    }



    P.S.
    Actually for values OR OR better to use radio buttons, not chekboxes

    P.P.S
    and view

    <?php if (get_post_meta($post->ID, '_checkbox_1', true)){ ?>
    one checked
    <?php } else { ?>
    two checked
    <?php }?>

    Previous versions of this answer: 06/26/10 at 4:13am | 06/26/10 at 4:16am | 06/26/10 at 4:18am | 06/26/10 at 4:20am

  • avatar
    Last edited:
    06/26/10
    4:15am
    Utkarsh Kukreti says:

    <?php
    add_action("admin_init", "admin_init");
    add_action('save_post', 'save_price');

    function admin_init(){
    add_meta_box("prodInfo-meta", "Product Options", "meta_options", "product", "side", "low");
    }

    function meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $price = unserialize($custom["price"][0]);
    $options = array(5, 10, 15);
    ?>
    <label>Price:</label>
    <?php foreach($options as $o): ?>
    <label><?php echo $o ?></label><input name="price[]" type="checkbox" value="<?php echo $o; ?>" <?php if(in_array($o, $price)) echo 'checked="checked"'; ?> />
    <?php endforeach; ?>
    <?php
    }

    function save_price(){
    global $post;
    update_post_meta($post->ID, "price", serialize($_POST["price"]));
    }
    ?>


    This will save all the checkboxes that have been checked.

    Previous versions of this answer: 06/26/10 at 4:12am | 06/26/10 at 4:13am | 06/26/10 at 4:15am

  • avatar
    Last edited:
    06/26/10
    4:38am
    Svilen Popov says:

    <?php
    add_action("admin_init", "admin_init");
    add_action('save_post', 'save_price');

    function admin_init(){
    add_meta_box("prodInfo-meta", "Product Options", "meta_options", "product", "side", "low");
    }


    function meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $price = $custom["price"][0];

    ?>
    <label>Price:</label><br />
    5 <input type="checkbox" <? if ($price == '5') echo 'checked="yes"'; ?> name="price" value="5" />
    10 <input type="checkbox" <? if ($price == '10') echo 'checked="yes"'; ?> name="price" value="10" />
    15 <input type="checkbox" <? if ($price == '15') echo 'checked="yes"'; ?> name="price" value="15" />



    <?php
    }
    function save_price(){
    global $post;
    update_post_meta($post->ID, "price", $_POST["price"]);
    }
    ?>

  • avatar
    Last edited:
    06/26/10
    4:11am
    Andrzej Zglobica says:

    I think this should do?

    <?php
    add_action("admin_init", "admin_init");
    add_action('save_post', 'save_price');

    function admin_init(){
    add_meta_box("prodInfo-meta", "Product Options", "meta_options", "product", "side", "low");
    }

    function meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $price = (bool) $custom["price"][0];
    ?>
    <label>Price:</label><input name="price" type="checkbox" <?php if ( $price ) {
    ?>checked="checked"<?php
    } ?> />
    <?php
    }

    function save_price(){
    global $post;
    update_post_meta($post->ID, "price", (bool) $_POST["price"]);
    }
    ?>

This question has expired.





Current status of this question: Completed