wp_dropdown_pages without submit button

Found a great tutorial: How to Create a Dropdown List of Specified Pages in a Widget (used to be a link, but article no longer exists). A portion of which I used to create my own drop down / select menu using the WP function wp_dropdown_pages.

The following code can be used in place of wp_dropdown_pages:

<form name="form1" id="form1" action="" method="get">
<?php $select = wp_dropdown_pages('show_option_none=Select%20a%20Page&depth=1&sort_column=menu_order&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><input type="submit" name="submit" value="view" /></noscript>
</form>

see comments below: you must be sure there are not linebreaks in the middle of expressions

You can use any of the usual wp_dropdown_pages parameters. The code above will list only top-level pages, and in menu_order (the integer entered in the order field of a given WP Page).

10 Responses to “wp_dropdown_pages without submit button”

  1. Salvedor says:

    This code puts 2 menus on top of each other (with the top one inactive), is there a way to only add one?

    • Interstice Solutions says:

      Hi, Salvedor. Sometimes this happens if there are extra linebreaks. For example, sometimes WP will add these to widget text. After pasting the code above, go in and make sure there are no spaces or linebreaks within the expressions themselves (what is between the “” quotation marks).

      For example, the wp_dropdown_pages line can be quite long. If the text window where you are pasting this is very narrow, the line may wrap, but be sure there are no actual linebreaks or spaces within this longer expression.

  2. Salvador says:

    Thank you Interstice Solutions – It works! I noticed though that each time you make a selection the menu defaults back to the ”show_option_none”, it it possible for it to display the current page ?

  3. John says:

    The dropdown breaks once you navigate to a subpage.

    Example: BLOGURL.com/About-Us/ – Selecting a top level page from the dropdown on the About-Us page would take them to…

    BLOGURL.com/About-Us/Top-Level-Page/

    The simple fix is this:
    <form action="” method=”get”> – That should be your form code. I’m not sure if it was the hyphen breaking it or the trailing slash at the end.

    I hope my four hours of work on that problem helps someone else. =]

  4. Ian says:

    Thanks for posting this! Works great!

    Ian

  5. Tanner says:

    I’ve noticed the above code still shows a duplicate menu and as per John’s note above, is still appending the url’s to existing URLS (causing subpages to to mess up)

    any fix for these issues?

    • Erin Bruce says:

      Hi, I haven’t tried this code in quite a while. Have you checked the other possible issues mentioned on this page?

      • Tanner says:

        I have – i was able to get rid of the duplicate dropdown by passing “‘echo’ => 0” in the variables for the call. But it’s still appending the url to the existing URL. anyone with a better knowledge of PHP than me – i could sure use the help. It’s the one remaining issue with this otherwise useful method.

  6. Tanner says:

    With a little bit of inquiry a user over at WPanswers was able to come up with a solution – the URL is here.
    http://wordpress.stackexchange.com/questions/126184/wp-dropdown-pages-auto-submit

Leave a Reply