Whitelisted options (12 posts)

If you're following the latest dev release hold back on upgrading to 1188 until you read this post and check your plugins.

The way options work in the backend has been overhauled. There is now a whitelist for each options page hardcoded in the options page. It's possible to add options to the whitelist if you're developing plugins but unfortunately this means that any old plugin that submits to /wp-admin/options.php will break.

New API:add_option_update_handler($option_group, $option_name, $sanitize_callback = '')
This function adds an option called $option_name to the group(or page) $option_group. Optionally, $sanitize_callback is the name of a function used to sanitize the option value before saving.

remove_option_update_handler($option_group, $option_name, $sanitize_callback = '')
This function removes an option in the same way as above.

There are another two functions, add_option_whitelist( $new_options, $options = '' ) and remove_option_whitelist( $del_options, $options = '' ) which are useful for adding and removing options in bulk, but they're mostly for internal use by the first two functions.
See the function mu_options() in wp-admin/includes/mu.php for an example of usage.

I have MU 2.6.1 and did exactly what chmac said here inside a mu-plugin, and it doesn't work.

It seems that at the time that add_option_update_handler is called, $whitelist_options is still empty, and then after the merge with my $new_whitelist_options, it gets overwritten along the way and my options are not visible.

Any ideas? Maybe inside a mu-plugin it's different than in a regular plugin?

The nonce action argument and the option_page input took some time to figure through the code (it kept failing on check_admin_referrer).

Anyway, the only weird thing now is, that when I access option.php directly, I get the following warning:

Warning: array_search() [function.array-search]: Wrong datatype for second argument in /home/oren/workspace/wordpress/wp-mu/wp-admin/includes/mu.php on line 498

When var_dumping $whitelist_options at that location, the 'blah' option group is missing. Since I don't access options.php directly I don't mind, but is there something else that is affected, or that i'm doing wrong?

@orensol Interesting, I haven't looked at options.php so I'm not sure what's going on in there. Sounds weird.

Without looking at the code, I'd guess that the add_option_update_handler() function doesn't get called properly, or doesn't create the option group. Maybe there's another filter that needs to be set to create the option group and populate it.

I'm just guessing though, don't have time to look into it right now. Let us know if you find a solution. :)

chmac, thanks for sharing this whitelist_options filter route. It seems to be the easiest way to register multiple options without having to resort to something like a foreach looping many times add_option_update_handler()...

To render the aditionally required form fields automatically, the function settings_fields('blah'); can be used. It creates the option_page as well as nonce and other hidden fields in one go.