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

MultiSelect Checkboxes - Options Page WordPress

  • SOLVED

For my plugin, I am creating an options page with all categories displayed as checkboxes. I would like the end user to have the option to select multiple checkboxes. I believe the source of the problem is that an options array is saved as a single ID.

Here is the options array for the categories:

$qsoptions = array (

array (
'name' => 'Categories',
'id' => $qsshortname.'select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

If I use the code below, I can select only one category at a time:

<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $qsoption) { ?>
<label for="<?php echo $qsoption; ?>">
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $qsoption; ?>" value="<?php echo $qsoption; ?>" <?php if ( get_option( $value['id'] ) == $qsoption) { echo ' checked="checked"'; } ?> /> <?php echo $qsoption; ?>
</label>
<?php } ?>


My update_option code is based on the ID:
if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach ( $qsoptions as $value ) {
if ( array_key_exists('id', $value) ) {
if ( isset( $_REQUEST[ $value['id'] ] ) ) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
}
}
}


QUESTION: How do I update to allow the end user to select (and save) multiple checkboxes, instead of just one? Thanks.


Answers (3)

2014-05-12

Francisco Javier Carazo Gil answers:

Hi Louise,

Probably, the name shouldn't be different for every one, it should be the same and in array:

<input type="checkbox" name="myname[]" id="<?php echo $qsoption; ?>" value="<?php echo $qsoption; ?>" <?php if ( get_option( $value['id'] ) == $qsoption) { echo ' checked="checked"'; } ?> /> <?php echo $qsoption; ?>

Later in the update part:
foreach($_REQUEST["myname"] as $myname)
// make the update


Louise comments:

Hi there, thanks for your response. I am trying to follow, and cannot get it to work. I updated the name field of my code as you suggested in the first part of the code.

<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $qsoption) { ?>
<label for="<?php echo $qsoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $qsoption; ?>" value="<?php echo $qsoption; ?>"
<?php if ( get_option( $value['id'] ) == $qsoption) { echo ' checked="checked"'; } ?> /> <?php echo $qsoption; ?>
</label>
<?php } ?>

Then the second part of my code, I am less certain what to do based on your response. Here is what I changed it to:

if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach ( $_REQUEST['myname'] as $myname ) {
if ( array_key_exists('id', $value) ) {
if ( isset( $_REQUEST[ $value['id'] ] ) ) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
}
}
}

Appreciate your help, thanks!


Francisco Javier Carazo Gil comments:

You can save it directly if your options array is an array:

if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
update_option( 'your_option_name', $_REQUEST['myname'] ] );
}
}


Louise comments:

Hello again - I have several checkbox options within my plugin options page - some are multiselect, and some are not. Therefore, I need flexibility when options are updated. Can you help me follow the logic of the below code all the way through ( with name="myname[]"):

f ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach ( $qsoptions as $value ) {
if ( array_key_exists('id', $value) ) {
if ( isset( $_REQUEST[ $value['id'] ] ) ) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
}
}
}

Thanks!


Francisco Javier Carazo Gil comments:

If you know the names you can do something like:

// for singular options
update_option("singular_option_a", $_REQUEST["singular_option_a"]);

// and the same for multiple options
update_option("multiple_option_a", $_REQUEST["multiple_option_a"]);

Later, when you read it, you will be able to do:
$my_multiple_option = get_option("multiple_option_a");
if(in_array($element, $my_multiple_option))
// do something for activated
else
// do something for disabled


Louise comments:

Hello again - Thanks for your help so far. If I understand you correctly, you are suggesting to list out each option separately? Such as "multiple_option_a" and "multiple_option_b" etc. Is there no way to do it more flexibly? I have several options, and would like to avoid listing each one out separately. Thanks.


Louise comments:

Hello again - Thanks for your help so far. If I understand you correctly, you are suggesting to list out each option separately? Such as "multiple_option_a" and "multiple_option_b" etc. Is there no way to do it more flexibly? I have several options, and would like to avoid listing each one out separately. Thanks.


Francisco Javier Carazo Gil comments:

There are more options but this one, is the most "ordered" one. In any other case, you are going to have problems when you save/load options from database and this is not going to be so easy understand the whole process.


Louise comments:

Hello - thanks for your help so far. I'm still not there. Can you take a look at my code below?

-When you say "if you know the names" of the options, do you mean the ID (i.e. "jqs_select_categories) ?
-Also, I believe get_option in the third area of the code is still based on the ID, which may not link up to "foreach($_REQUEST['myname'] as $myname)...." Thanks!

array (
'name' => 'Categories',
'id' => 'jqs_select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach($_REQUEST['myname'] as $myname){
if ( isset( $_REQUEST['myname'] ) ) {
update_option( 'jqs_select_categories', $_REQUEST['myname'] );
} else {
delete_option( 'jqs_select_categories' );
}
}


<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?>"
<?php if ( get_option( $value['id'] ) == $FPFoption) { echo ' checked="checked"'; } ?> />
<?php echo $FPFoption; ?>
</label>


Francisco Javier Carazo Gil comments:

-When you say "if you know the names" of the options, do you mean the ID (i.e. "jqs_select_categories) ?
-Also, I believe get_option in the third area of the code is still based on the ID, which may not link up to "foreach($_REQUEST['myname'] as $myname)...." Thanks!

The answer for this two is that we should have here the whole files to see it but, yes, I have seen somes WooCommerce categories here you can:
* Save an array in options with all categories selected by user
* Make a checked function based in this array to see which inputs is going to be checked
* Make a loop with categories (you have it)
* The save action should take all the values given and update it correctly with update_options


Louise comments:

Hello - You'd like to see the code for $woo_categories?

global $woo_categories;
$woo_categories = array();
$woo_categories_args = array('hide_empty' => 0, 'builtin' => false);
$woo_categories_obj = get_categories($woo_categories_args);
foreach ($woo_categories_obj as $woo_cat) {
$woo_categories[$woo_cat->cat_ID] = $woo_cat->cat_name;}
$categories_tmp = array_unshift($woo_categories);

I don't have any issues displaying the categories or checking just one box. Can you help me with the code I posted before? What am I missing?


Louise comments:

Hi - I appreciate any more help you can offer. Thanks!


Francisco Javier Carazo Gil comments:

I try:


array (
'name' => 'Categories',
'id' => 'jqs_select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
if ( isset( $_REQUEST['myname'] ) ) {
update_option( 'jqs_select_categories', $_REQUEST['myname'] );
} else {
delete_option( 'jqs_select_categories' );
}
}


<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?>"
<?php if ( get_option( $value['id'] ) == $FPFoption) { echo ' checked="checked"'; } ?> />
<?php echo $FPFoption; ?>
</label>


Louise comments:

Hello again - That didn't work. With this code, the "jqs_select_categories" does not show up in my wp-options table in the database. However, I can print out the results of "myname" successfully. The option is not making it to the database?


Francisco Javier Carazo Gil comments:

Insert a var_dump and a die() and tell me which is shown.



else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){

if ( isset( $_REQUEST['myname'] ) ) {

update_option( 'jqs_select_categories', $_REQUEST['myname'] );
var_dump( $_REQUEST['myname'] );
die();

} else {

delete_option( 'jqs_select_categories' );

}

}



Louise comments:

Sure - Here is the output :

array(3) { [0]=> string(4) "Bath" [1]=> string(7) "Kitchen" [2]=> string(5) "Patio" }


My categories correctly displayed and then I selected 3 checkboxes - Bath, Kitchen & Patio.


Louise comments:

Sure - Here is the output :

array(3) { [0]=> string(4) "Bath" [1]=> string(7) "Kitchen" [2]=> string(5) "Patio" }


My categories correctly displayed and then I selected 3 checkboxes - Bath, Kitchen & Patio.


Louise comments:

Also, it now display in the database - a:3:{i:0;s:9:"Going Out";i:1;s:7:"Kitchen";i:2;s:5...


Francisco Javier Carazo Gil comments:

Ok, perfect, so now you have to do a checked function here:


<h3><?php echo $value['name']; ?></h3>

<?php $categories = get_option("your_categories"); // set the good name ?>

<?php foreach ($value['options'] as $FPFoption) { ?>

<label for="<?php echo $FPFoption; ?>">

<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?> <?php if(in_array($FPFoption, $categories)) echo 'checked'; ?>"

<?php echo $FPFoption; ?>

</label>


Louise comments:

Almost there, thank you!! !!!

-My message on "save" no longer displays - i.e. "Plugin Settings Saved."

-Also, If I want to call this option later, though, I am still not sure how to use get_option?


Francisco Javier Carazo Gil comments:

Perfect :)

Inline:

-My message on "save" no longer displays - i.e. "Plugin Settings Saved."
You can do the echo after update_option:
echo '<div id="message" class="updated fade"><p>Plugins settings saved'</p>

-Also, If I want to call this option later, though, I am still not sure how to use get_option?
You will have (you can use it wherever you want in your code):

$categories = get_option("option_name");

You will have an array and you could access it in the standard way $categories[0], $categories[1]...


Louise comments:

Thank you!!!

2014-05-12

Sébastien | French WordpressDesigner answers:

When you want to create a multi-select element, you create a set of check boxes instead of a list box. When you specify the check boxes give them all the same name.


Louise comments:

Hello - Can you offer any assistance with my code below?



Louise comments:

This is my latest attempt, but it doesn't work:

array (
'name' => 'Categories',
'id' => 'jqs_select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach($_REQUEST['myname'] as $myname){
if ( isset( $_REQUEST['myname'] ) ) {
update_option( 'jqs_select_categories', $_REQUEST['myname'] );
} else {
delete_option( 'jqs_select_categories' );
}
}


<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?>"
<?php if ( get_option( $value['id'] ) == $FPFoption) { echo ' checked="checked"'; } ?> />
<?php echo $FPFoption; ?>
</label>


Sébastien | French WordpressDesigner comments:

what's the output of this code please ?


Louise comments:

The categories all correctly display on the menu page, but when I press "Save," the options are not saved. I made a shortened version of my code:

https://gist.github.com/lmcguire17/acce25561ccd4d6c7c24


Louise comments:

Hi - I still haven't figured it out if you can offer any more assistance?

2014-05-12

Firoja_Imtosupport answers:

<input type="checkbox" id="selectall"><label for="selectall">Select All</label>
<?
$cntLoop=0;
$value['options']=$choices;
foreach ($choices as $key => $value)
{
$cntLoop++;
echo "<input type='checkbox' value=\"".$value."\" name=\"field_5289a9430ee82[]\" class=\"checkbox case\" id=\"acf-field-ambiance_".$cntLoop."\"><label for =\"acf-field-ambiance_".$cntLoop."\">".$value.'</label><br />';
}
?>

Then serialize the data after submit and you can update


Firoja_Imtosupport comments:

Hi,

I had attached image of what i had done locally, please canu share ur skype id with me


Louise comments:

Hello - Can you offer an assistance with my updated code?


Firoja_Imtosupport comments:

Hi,

of course i can assist you, please can you write your updated code here again?

Thanks


Louise comments:

Sure here it is:

array (
'name' => 'Categories',
'id' => 'jqs_select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach($_REQUEST['myname'] as $myname){
if ( isset( $_REQUEST['myname'] ) ) {
update_option( 'jqs_select_categories', $_REQUEST['myname'] );
} else {
delete_option( 'jqs_select_categories' );
}
}


<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?>"
<?php if ( get_option( $value['id'] ) == $FPFoption) { echo ' checked="checked"'; } ?> />
<?php echo $FPFoption; ?>
</label>


Firoja_Imtosupport comments:

Hi,

One thing i noticed is your ID is static, Please can you change your code as below:
array (
'name' => 'Categories',
'id' => $shortname.'_jqs_select_categories',
'type' => 'checkbox',
'options' => $woo_categories,
),
);

Let me know if this works, meanwhile i will still see your code in detail.

Thanks


Firoja_Imtosupport comments:

$qsoptions[]=array("name"=>"Categories",
"id" => $shortname."_jqs_select_categories",
"options" => $woo_categories,
"type" => "checkbox");


Louise comments:

Apologies, that was a copy and paste error. It is correct in my code. Here is the code I am using:

https://gist.github.com/lmcguire17/acce25561ccd4d6c7c24


Louise comments:

Apologies, that was a copy and paste error. It is correct in my code. Here is the code I am using:

https://gist.github.com/lmcguire17/acce25561ccd4d6c7c24


Firoja_Imtosupport comments:

No problem, Can i know what issue you are facing now?


Louise comments:

Yes, the code is not working. I am able to get all of the categories to appear in the menu, but when I click "save," none of my options are saved. I also posted the full code if it helps. I believe the issues are in this code:

else if ( isset ($_REQUEST['action']) && ( 'save' == $_REQUEST['action'] ) ){
foreach($_REQUEST['myname'] as $myname){
if ( isset( $_REQUEST['myname'] ) ) {
update_option( 'jqs_select_categories', $_REQUEST['myname'] );
} else {
delete_option( 'jqs_select_categories' );
}
}


<h3><?php echo $value['name']; ?></h3>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="myname[]" id="<?php echo $FPFoption; ?>" value="<?php echo $FPFoption; ?>"
<?php if ( get_option( $value['id'] ) == $FPFoption) { echo ' checked="checked"'; } ?> />
<?php echo $FPFoption; ?>
</label>


Firoja_Imtosupport comments:

Hi,

I had sent you a PM.

Thanks