Ask your WordPress questions! Pay money and get answers fast! (more info)

Multiple options not staying selected after saving WordPress

  • SOLVED

I am displaying a list of all published pages just fine. I am echoing out the selected options just fine. It just doesn't seem to be saving.

The select fields:

<select id="exclude_page_from_cookies" name="exclude_page_from_cookies[]" multiple="multiple">
<?php

$pages = get_pages();
foreach ( $pages as $page ) {
$title = get_the_title($page);
$id = $page->id;
?>

<option id="<?php echo $id; ?>" value="<?php echo $title ?>" <?php selected( $title ); ?> >
<?php echo $title;?>
</option>
<?php
}
?>
</select>

The save function:

if ( isset( $_POST['exclude_page_from_cookies'] ) ) {
foreach( $_POST['exclude_page_from_cookies'] as $exclude_page ) {
echo $exclude_page;
update_option( 'exclude_page_from_cookies', $exclude_page ) ;
}
}


*****EDIT*****

The save works, but is only storing one of the options in the database.

I would like the options to stay selected AFTER saving.

Answers (3)

2014-05-18

Hariprasad Vijayan answers:

Hello,

Are you getting any output from <em><strong>echo $exclude_page;</strong></em> ?


Hariprasad Vijayan comments:

Hello,

You can only store one row in wp_options table. Here you need to store array of elements. Here you need to store array in wp_options table.

Try following code for saving

if ( isset( $_POST['exclude_page_from_cookies'] ) ) {
update_option( 'exclude_page_from_cookies',serialize($_POST['exclude_page_from_cookies']) ) ;
}


try following code for select field

<select id="exclude_page_from_cookies" name="exclude_page_from_cookies[]" multiple="multiple">
<?php
$exclude_page_from_cookies = unserialize(get_option( 'exclude_page_from_cookies'));
$pages = get_pages();
foreach ( $pages as $page ) {
$title = get_the_title($page);
$id = $page->id;
?>

<option id="<?php echo $id; ?>" value="<?php echo $title ?>" <?php if (in_array($id, $exclude_page_from_cookies)){ echo ' selected'; } ?> >
<?php echo $title;?>
</option>
<?php
}
?>
</select>

Let me know if you have trouble.

Regards,
Hariprasad


Hariprasad Vijayan comments:

Is that code works for you?


Dsmart comments:

Haripasad,

Thank you, it works in saving the array of selected options into the table, however after saving it is still not showing the selected items as selected. This is what it looks like before I save http://grab.by/wZca, but after I save it reverts to an empty field http://grab.by/wZcc


Hariprasad Vijayan comments:

Okay. Try this


<select id="exclude_page_from_cookies" name="exclude_page_from_cookies[]" multiple="multiple">
<?php
$exclude_page_from_cookies = unserialize(get_option( 'exclude_page_from_cookies'));
$pages = get_pages();
foreach ( $pages as $page ) {
$title = get_the_title($page);
$id = $page->id;
?>

<option id="<?php echo $id; ?>" value="<?php echo $title ?>" <?php if (in_array($title, $exclude_page_from_cookies)){ echo 'selected'; } ?> >
<?php echo $title;?>
</option>
<?php
}
?>
</select>


Dsmart comments:

Nailed it. Thanks for bearing with me. I am new at this :)

2014-05-18

Just Me answers:

How do you know it is not saving? Did you check the database or did you expect the first dropdown menu to reflect the selected choices or do the selected pages still show up somewhere?

If either of the later ones is true, you will have to add some commands to read back the settings.
Something like

//this will not display the selected pages
$exclude_pages = get_option('exclude_page_from_cookies');
$pages = get_pages();
foreach ( $pages as $page ) {
$title = get_the_title($page);
if ( ! in_array( $title, $exclude_pages) ) {
$id = $page->id;
and so on

If you were to store page ids instead of title names you could just change the command
$pages = get_pages();
to
$pages = get_pages(array(exclude => $exclude_pages) );
and would not have to check the pages

//if you do want them to show up in the menu so they can be deselected
Another scenario would be to do the in_array check at the point where you output the option list
in that case you would perform a test like
if in_array( $title, $exclude_pages) checked=checked

This is all pseudo code you will have to finetune it to make it work, but I hope you get the idea.


Dsmart comments:

Thank you,

I have checked the database and it turns out that only one of the selected options is saving. I think this is because they are all assigned the same id though. I could use help there, as well as expanding on your last suggestion to keep them shown as selected in the menu so they can be deselected.


Just Me comments:

what does the selected( $title ) function do?

2014-05-18

Bob answers:

I think there is problem with your saving. your are overwriting the option in loop.
below code will loop through all selected pages and then it will overwrite <strong>exclude_page_from_cookies</strong> each time.

if ( isset( $_POST['exclude_page_from_cookies'] ) ) {
foreach( $_POST['exclude_page_from_cookies'] as $exclude_page ) {
echo $exclude_page;
update_option( 'exclude_page_from_cookies', $exclude_page ) ;
}
}

you should save serialized value/array.


Bob comments:

[[LINK href="http://codex.wordpress.org/Function_Reference/update_option"]]update_option[[/LINK]] accepts array as it's value so you can pass an array.

if <em>$_POST['exclude_page_from_cookies']</em> is returning the array you want then you can save it directly

$excludepages = $_POST['exclude_page_from_cookies'];
update_option( 'exclude_page_from_cookies', $excludepages );


it will return array of exclude pages.
get_option( 'exclude_page_from_cookies' );


Bob comments:

To make your life easy you can use plugin like [[LINK href="http://wptheming.com/options-framework-plugin/"]]options framework[[/LINK]]

or

this sample plugin with minimal setup [[LINK href="http://wp-lovers.com/wpquestions/wlseo-new.zip"]]http://wp-lovers.com/wpquestions/wlseo-new.zip[[/LINK]]
This plugin help you to create different sections and form fields in those sections
It has only two files one for all necessary code and options you want to create and another is css! {Let me know if you can not understand in it.}

There are also other plugin which you might be interested like [[LINK href="https://wordpress.org/plugins/admin-page-framework/screenshots/"]]Admin Page Framework[[/LINK]]

If you are creating theme and you have lot's of option then [[LINK href="https://upthemes.com/upthemes-framework/"]]uptheme framework[[/LINK]] is also good.

and the last on [[LINK href="https://github.com/bainternet/Admin-Page-Class"]]Admin Page Class[[/LINK]] will be also hlepful to you.