Add option for editors through `register_setting`

I want to add some options in wordpress admin panel which editors also can update through a form. I have used register_setting() function to add those options.

For administrator this works fine, but for editors i get message Sorry, you are not allowed to manage these options.. Is there a way so editors can also edit these new options?

1 Answer
1

To allow users with capabilities other than manage_options to save a setting you can use the option_page_capability_{$option_page} hook. Despite the name, {$option_page} is the option group, which is what you set for the first argument of register_setting() and settings_fields() on your options page.

So if you registered a setting:

register_setting( 'wpse_310606_setting_group', 'wpse_310606_setting' );

You can allow other capabilities to save it with:

function wpse_310606_setting_capability( $capability ) {
    return 'edit_pages';
}
add_filter( 'option_page_capability_wpse_310606_setting_group', 'wpse_310606_setting_capability' );

Editors and Administrators both have the edit_pages capability, so this would allow both roles to save this settings group.

Leave a Comment