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):
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:
wp_dropdown_pages( array( 'name' => 'plugin_options[whatever_page]', 'show_option_none' => __( '— Select —' ), 'option_none_value' => '0', 'selected' => $options['whatever_page'] ) );
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!