Display the stored selectbox value in the selectbox after refresh on a WordPress options page

StackOverflow https://stackoverflow.com/questions/17404591

Domanda

I have created a WordPress options page and on that page I want a selectbox in which I can select a page from my website. The following options code does that perfectly! I select a page, hit the save button and it stores the correct value.

My problem is that when I save my page (or refresh it) the selectbox does not show the selected and stored value, but the first in the list. How can I make it so that the checkbox shows the stored value?

<?php break; case 'select_pages':   ?>
<div class="options_input">
    <label class="option_title" for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label><br>
    <select name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>">
        <option value=""><?php echo esc_attr( __( '&mdash; Select &mdash;' ) ); ?></option> 
       <?php
            $page_selected = $options[$valueid];
            $pages = get_pages();
            foreach ( $pages as $page) :
        ?>
        <option value="<?php echo $page->post_title; ?>" <?php if ( $page_selected && in_array( $page->post_title, $page_selected ) ) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>
        <?php endforeach; ?>
    </select>
    <?php $cc_responsive_theme_options = get_option('cc_responsive_theme_options'); echo $cc_responsive_theme_options['footer_link_1']; ?>
    <span class="option_desc"><?php echo $value['desc']; ?></span>
</div>
È stato utile?

Soluzione

Accidentally found the solution. The checkbox was set to multiple and it did not compare the stored value to the one in the selectbox.

This line:

<option value="<?php echo $page->post_title; ?>" <?php if ( $page_selected && in_array( $page->post_title, $page_selected ) ) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>

has to be changed to this:

<option value="<?php echo $page->post_title; ?>" <?php if( $page_selected == $page->post_title) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top