0

I need to get a list of all pages in a dropdown list, so I can get the page id of the selected page to store in the options of my plugin.

Right now I have:

?>
    <input name='wpplf23_plugin_options[thankyou_page]' type='number' value='<?php if ( ( isset( $options['thankyou_page'] ) ) ) { echo $options['thankyou_page']; } ?>' />
<?php

To just store the id manually.

I found this code:

$args = array(
    'depth'                 => 0,
    'child_of'              => 0,
    'selected'              => 0,
    'echo'                  => 1,
    'name'                  => 'page_id',
    'id'                    => null, // string
    'class'                 => null, // string
    'show_option_none'      => null, // string
    'show_option_no_change' => null, // string
    'option_none_value'     => null, // string
    );

    wp_dropdown_pages( $args );

But I cant work out how to get it to do what I need. How would I go about this?

Thanks for any help you can provide me.

1
  • Wp_dropdown_pages() works well for your case, you need to fill the args with the right value. Ie : selected => get_option('wpplf23_plugin_options['thankyou_page']')
    – Benoti
    Commented Dec 30, 2016 at 9:22

2 Answers 2

2

Iam using following code to display a page select dropdown menu on an plugin option page.

If you select an page and save it, the next time you visit the option-site, you will also see which page was saved. (so no more "select page")

You can use the WordPress selected() function, you can find more details here: https://codex.wordpress.org/Function_Reference/selected

$options = get_option( 'my_settings' ); ?>

<select name='my_settings[selected_page]'>
    <option value='0'><?php _e('Select a Page', 'textdomain'); ?></option>
    <?php $pages = get_pages(); ?>
    <?php foreach( $pages as $page ) { ?>
        <option value='<?php echo $page->ID; ?>' <?php selected( $options['selected_page'], $page->ID ); ?> ><?php echo $page->post_title; ?></option>
    <?php }; ?>
</select>

You just need to get the current-saved-value of the field and than use the WP selected() function to compare the current-saved-value with the $page->ID.

Maybe this can help you.

1

Try below code.

<select name="page"> 
    <option value=""><?php echo esc_attr( __( 'Select page' ) ); ?></option> 
    <?php 
     $pages = get_pages(); 
     foreach ( $pages as $page ) {
       $option = '<option value="' . $page->ID . '">'.$page->post_title.'</option>';
       echo $option;
     }
    ?>
</select>

Hope this will helps you.

1
  • It does get the posts in a list but it doesn't recall it if the option is set it always says "Select page"
    – Rikki
    Commented Dec 30, 2016 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.