Plugin Options Pages in WordPress 2.7

WordPress 2.7 requires that plugins explicitly white list their options using a couple new functions. WordPress MU has required this security measure for a while, and it’s nice to see an evolved form of it brought to the core code. Migrating Plugins and Themes to 2.7 article in the codex offers some guidance, but here’s how it works:

First, register each option for your plugin during the admin_init action:

function myplugin_admin_init(){
	register_setting( 'my-options-group', 'my-option-name-1', 'absint' );
	register_setting( 'my-options-group', 'my-option-name-2', 'wp_filter_nohtml_kses' );
}
add_action( 'admin_init', 'myplugin_admin_init' );

In the example above, the value for my-option-name-1 will be filtered by absint before being saved to the options table. my-option-name-2 will be stripped of any HTML by wp_filter_nohtml_kses.

Then build a form like this prototype:

<form method="post" action="options.php">
 
<?php settings_fields('my-options-group'); ?>
 
<input name="my-option-name-1" id="my-option-name-1" type="checkbox" value="1" <?php checked('1', get_option('bsuite_insert_related')); ?> />
 
<input name="my-option-name-2" id="my-option-name-2" type="text" value="<?php format_to_edit( get_option( 'bsuite_insert_related' )) ?>" />
 
<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" class="button" />
 
</form>

Easy.

8 Responses to “Plugin Options Pages in WordPress 2.7”

  1. Thanks, this helped a lot!

  2. Most interesting.Great idea.

  3. Just what I needed, thanks!

  4. wow, easy huh? lol, I have no idea where each code goes to which file. I know I am working with the options.php missing when I set my plugins only 2 settings which is user name and password. Do I edit the plugins options file along with wp_options file or is it just the plugins file I edit?

  5. The docs for the 2.7 settings are so bad. And it seems that the “new settings api” (http://codex.wordpress.org/Settings_API) does not work with custom settings pages. Thanks to your post at least I know how to do it without the settings api :-/

  6. Hi there, just a quick question concerning the Survey plugin, I’ve just started a blog and would like the visitors to post a survery answer but will they be able to see the results of the survey online in real time. I think that would be a great idea because it increases reader interaction> Many thanks Peter.

Trackbacks

User contributed tags for this post:

wordpress craigslist plugin (74) - register_settings (30) - register_setting wordpress (12) - wordpress whitelist options (7) - craigslist wordpress plugin (5) - craigslist plugin for wordpress (3) - wordpress private pages (3) - register_setting no save options wordpress (3) - craigslist posting wordpress plugin (2) - wordpress plugin options (2) - register_setting example (2) - craigslist plugin wordpress (2) - example register_setting (2) - wordpress register_setting (2) - register_setting (2) - wordpress register_settings (2) - wordpress survey plugin (2) - private pages wordpress (2) - wordpress (2) - pages in wordpress (2) - popular post plugin wordpress (1) - wordpress plugin register_settings (1) - wordpress plugin options page table (1) - sample register setting wordpress (1) - wordpress 2.7 plugin option (1) - admin_init- wordpress (1) - wordpress register_setting() example (1) - register_settings() (1) - wordpress register_setting example (1) - wordpress admin_init (1) - post page to craiglist wordpress (1) - wordpress plugin options page (1) - wordpress plugin settings page (1) - daniel shorten (1) - wpmu registration settings (1) - wordpress whitelisted option group (1) - wordpress register setting example (1) - create form in the option page of wordpress plugin (1) - whitelist_options wpmu compatibility (1) - wordpress plugin craigslist (1) - register_settings wordpress (1) - option page wordpress 2.7 (1) - survey plugin wordpress (1) - wordpress private post plugin (1) - wordpress private blog (1) - wordpress private plugin (1) - inurl:blog/post (1) - wordpress private comments (1) - gallery2 flickr plugin (1) - posting to pages in wordpress (1) - tables in wordpress post (1) - wordpress pages and posts plugin (1) - wordpress, craigslist (1) - register settings wordpress (1) - wordpress register settings array (1) - admin_init wordpress (1) - register_setting wordpress tutorial (1) - admin_init (1) - where to find the plugin option in wordpress (1) - widget (1) - wordpress register_setting array (1) - wordpress craigslist (1) - wordpress whitelist_options (1) - plugin options (1) - saving plugin settings wordpress register_setting (1) -