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

Custom Meta Box Checkbox Issue WordPress

  • SOLVED

I have registered the following Checkbox in a Custom Meta Box.
It works however I want to tick multiple boxes, when I click three it will only save one, usually the first checked.

So I am after a solution to this issue.

echo'<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

echo '<input type="checkbox" name="accreditations" value="aaa_tourism"';
if($accreditations == 'aaa_tourism'){echo 'checked';}else{echo '';};
echo' /> AAA Tourism';

echo '<input type="checkbox" name="accreditations" value="accredited_tourism_business_australia"';
if($accreditations == 'accredited_tourism_business_australia'){echo 'checked';}else{echo '';};
echo '/> Accredited Tourism Business Australia ';

echo '<input type="checkbox" name="accreditations" value="environmentally_friendly"';
if($accreditations == 'environmentally_friendly'){echo 'checked';}else{echo '';};
echo '/> Environmentally Friendly';

Answers (3)

2011-03-31

AdamGold answers:

The HTML code:
<?php



echo '<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';



?>

<input type="checkbox" name="accreditations[]" value="aaa_tourism" <?php checked( in_array( 'aaa_tourism', $accreditations ) ); ?> /> AAA Tourism



<input type="checkbox" name="accreditations[]" value="accredited_tourism_business_australia" <?php checked( in_array( 'accredited_tourism_business_australia', $accreditations ) ); ?> /> Accredited Tourism Business Australia



<input type="checkbox" name="accreditations[]" value="environmentally_friendly" <?php checked( in_array( 'aaa_tourism', $environmentally_friendly ) ); ?> /> Environmentally Friendly


The PHP code:

$accreditations = $_POST['accreditations'];
$i = 0;
foreach( $accreditations as $value ) {
update_option('accreditation' . $i, $value);
$i++;
}

It will save your checkboxes in the database, by accreditation1, accreditation2 and so on..

2011-03-31

Lew Ayotte answers:

You need to make sure $accreditations is an array value that contains any of the checked values... then you need to set the input name to accreditations[] (to signify the HTML can contain multiple values). When you check, simply do the in_array function. By the way, WordPress has a nifty helper function called "checked" it will output the proper HTML if the value is true. You're output "checked" isn't valid HTML in all browsers...

See this code:

<?php

echo '<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

?>
<input type="checkbox" name="accreditations[]" value="aaa_tourism" <?php checked( in_array( 'aaa_tourism', $accreditations ) ); ?> /> AAA Tourism

<input type="checkbox" name="accreditations[]" value="accredited_tourism_business_australia" <?php checked( in_array( 'accredited_tourism_business_australia', $accreditations ) ); ?> /> Accredited Tourism Business Australia

<input type="checkbox" name="accreditations[]" value="environmentally_friendly" <?php checked( in_array( 'aaa_tourism', $environmentally_friendly ) ); ?> /> Environmentally Friendly


parksey18 comments:

So what would the code be for a custom meta box?
As when I replace the code it is full of errors?


Lew Ayotte comments:

I disagree with AdamGold's PHP code... I would do this:



if ( isset( $_POST['accreditations'] ) ) {
update_option( 'accreditation', $_POST['accreditations'] );
}


WordPress will automatically serialize the array into one option.

You're probably getting errors because your variable isn't initialized properly. I'd have to see your actual code block to tell you what you've done wrong.

2011-03-31

Denzel Chia answers:

Hi,

Using name="accreditations[]" for your checkbox is correct.
This will post the values as an array to your save post meta function, and it will be saved as an array.
To avoid PHP error, i assume that you use
$accreditation = get_post_meta($post_id,'accreditation',true);
to retrieve your post meta value.

You need to use foreach to loop the array before assigning back to your checkbox to determine which box to check. This also applies to your single.php where you are echoing out the values!

I am not sure what are your codes, therefore I can only assume and give you examples.

For your form it should be something like this, since your post meta is an array value.


<?php
global $post;
$post_id = $post->ID;
$acc = array();
$acc = get_post_meta($post_id,'accreditations',true);

echo'<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

echo '<input type="checkbox" name="accreditations" value="aaa_tourism"';

foreach($acc as $accreditations){
if($accreditations == 'aaa_tourism'){echo 'checked';}else{echo '';};
}

echo' /> AAA Tourism';

echo '<input type="checkbox" name="accreditations" value="accredited_tourism_business_australia"';

foreach($acc as $accreditations){
if($accreditations == 'accredited_tourism_business_australia'){echo 'checked';}else{echo '';};
}

echo '/> Accredited Tourism Business Australia ';

echo '<input type="checkbox" name="accreditations" value="environmentally_friendly"';

foreach($acc as $accreditations){
if($accreditations == 'environmentally_friendly'){echo 'checked';}else{echo '';};
}

echo '/> Environmentally Friendly';

}
?>



And for your single.php, it should be something like this.


<?php

//for echo out accreditations
//assuming post meta key is accreditations

global $post;
$post_id = $post->ID;
$accreditations = get_post_meta($post_id,'accreditations',true);
$template_url = get_bloginfo('template_url');

foreach($accreditations as $accres){

switch ($accres) {
case "aaa_tourism":
echo "<img src='$template_url/images/logo-aaa-tourism.png' title='AAA Tourism'>";
break;
case "accredited_tourism_business_australia":
echo "<img src='$template_url/images/logo-atba.png' title='Accredited Tourism Business Australia'>"; break;
case "environmentally_friendly":
echo "<img src='$template_url/images/logo-environmentally-friendly.png' title='Environmentally Friendly'>";
break;
}
}
?>


Thanks.