I'm developing a plugin with an options page using the Settings API.

I'd like to have one options array stored for my plugin, but on the settings page, I'd only like some of the settings to be visible to admin users, but the full list of settings to be available to the super admin.

Is this possible?

2 Answers 2


Test the current user’s role with current_user_can( 'administrator' ):

if ( current_user_can( 'administrator' ) )
    add_settings_field( /* arguments */ );
    // or
    add_settings_section( /* arguments */ );

Make sure to use the same check when you save the options. Otherwise your regular users might delete the values.

  • I'm not sure that this would play nicely with do_settings_sections(), unless administrator-only settings were given their own settings section. Nov 20, 2012 at 18:23
  • The callback for do_settings_fields() can include that test.
    – fuxia
    Nov 20, 2012 at 18:26
  • Ooooh, that would be nicely elegant! Though, it might work even better in the add_settings_field() callback. Nov 20, 2012 at 18:28
  • Thanks @toscho -- I've got the display working correctly now! Where would you suggest I do the check when I save the options.. in the callback specified in register_setting()?
    – jessica
    Nov 20, 2012 at 18:45
  • Also, if I'm using do_settings_fields() instead of do_settings_sections() -- I take it that I need to manually print the section titles and the table opening and closing tags, correct? Or is there a more elegant way to do this?
    – jessica
    Nov 20, 2012 at 18:49

Alternately, you could incorporate current_user_can() into the add_settings_field() callback for each admin-only option:

function somesetting_add_settings_field( $option ) {
    // This is an admin-only option
    if ( current_user_can( 'administrator' ) {
        // If current user is admin,
        // output the form field
    } else {
        // Otherwise, output a message,
        // or display a static value of the option,
        // etc.

Using this method, you shouldn't have to mess with your actual settings page markup at all.

If your sanitization callback is written properly, such that you check for data to be set, and array_merge() validated/sanitized values with existing option values, you shouldn't have to worry about any admin-only settings being deleted or improperly overwritten.

  • It seems like this solution wouldn't output any editable fields for non admin users, amirite?
    – jessica
    Nov 20, 2012 at 19:27
  • Oh, are you saying to only include this callback for the admin-only settings, and use a different callback for the settings available to regular users?
    – jessica
    Nov 20, 2012 at 19:31
  • Each setting uses its own callback. If you build things dynamically using an array of arrays of options, then just add an admin-only option parameter, and test for it with the current_user_can() conditional. Nov 20, 2012 at 19:36
  • Cheers, this would also work. However, I think I'm going with @toscho's answer because I believe it'd take less code to just conditionally add the admin-only settings section. I wouldn't have to change the markup with that solution either (not sure why he mentioned using 'do_settings_fields()' in the comments, as it turns out it isn't necessary).
    – jessica
    Nov 20, 2012 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.