I want a select-dropdown field to display the last saved option in the database; I started with this video from YT, https://www.youtube.com/watch?v=hbJiwm5YL5Q&t=3969s and got as far as 1:05:00 and this is my current version

function selectpageHTML() {?>
            <select name="lp_actPage"> 
                <?  $pages = get_pages();
                        foreach ( $pages as $page ) {                               
                                $slug = $page->post_name;
                                $title = $page->post_title;
                                echo "<option value='$slug'" . selected(get_option('lp_actPage', $slug)) .">$title</option>";
                        } ?>

I also looked at Select dropdown not showing selected value php, but instead of comparing floats I'm looking to compare strings.

  • you should replace all instances of <? with <?php, short tags are deprecated and in newer versions of PHP don't work at all. You should also avoid directly embedding variables into quotes the way that code does with $slug as it's not possible to apply escaping and is a great way to inject malicious HTML. It should be echo '<option value="' . esc_attr( $slug ) . '" '.
    – Tom J Nowell
    Aug 9, 2022 at 16:24

1 Answer 1


The problem is this:

selected(get_option('lp_actPage', $slug))

selected needs 2 parameters, but you gave it 1, and get_option wants 1 parameter but you gave it 2.

If we reformat this, the problem becomes much clearer:

    get_option('lp_actPage', $slug)

What's necessary is:



get_pages doesn't use the normal post querying code, and doesn't benefit from the same enhancements and plugin integration you get from other functions.

Instead of get_pages, consider:

$pages = get_posts( [
    'suppress_filters' => false, // enable caching
    'post_type' => 'page', // we want pages
    'post_status' => 'publish', // speed up by asking for only published posts
    'posts_per_page' => 100,
] );

Likewise, don't use <? use <?php or your code will break when your host upgrades PHP in the future.

  • Thanks, Tom, your solution is working, and I fixed my coding schema per your recommendation Aug 9, 2022 at 23:34

Your Answer

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

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