Easily include a page dropdown in your plugin or theme’s settings

Ok, so here’s a common story I’ve seen too many times, and admittedly been guilty of myself (the following is a screenshot from a plugin I made a while back):

Making the user go and look for page ID’s themselves? Rather embarrassing.

This is even worse, considering the fact that it can be solved with one line of code.

What is that magic function I speak of? wp_dropdown_pages. It’s super-handy, super-easy to use, and blends in so well with settings code. Here’s a quick dirty usage example:

As should be quite clear, you set the name parameter to be the field’s name (you are using a single array as the settings for your plugin, right?) and the selected value to be whatever value is currently selected. You know the best part? It should work seamlessly even if you’ve been doing this wrong – it will set the setting to the page ID. Silly me for not realising this earlier.

P.S. I know I haven’t posted in a good, good while (damn you, A-levels!), but hopefully I should get some more tips and tutorials out for you guys, so stay tuned!

  • Gstricklind


     Any way of using this to grab the page’s url rather than ID? Thanks for posting!

    • WPandBacon


       Sure, just wrap the value you get from this in a get_permalink() call when you’re actually using it. (Or, you can do it in the settings validation function, if you just want to cache the string.)

  • Rahim


    Thanks alot for the code, It works like charm. except that after selected a page, after refresh options the page is not selected. is there anyway to keep the page selected? I really appreciate your help.