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 thoughts on “Plugin Options Pages in WordPress 2.7

  1. Pingback: Daniel Shorten » Plugin Options in WordPress 2.7

  2. 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?

  3. Pingback: Plugin Options in WordPress 2.7 - Daniel Shorten

  4. 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.

Comments are closed.