Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Modifying function for checkboxes WordPress

  • SOLVED

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

Answers (4)

2010-06-26

Oleg Butuzov answers:

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<strong> radio buttons, not chekboxes</strong>

P.P.S
and view

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

2010-06-26

Svilen Popov answers:

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

2010-06-26

Utkarsh Kukreti answers:

<?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.

2010-06-26

Andrzej answers:

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