Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
I'm not sure that this would play nicely with do_settings_sections(), unless administrator-only settings were given their own settings section. –  Chip Bennett Nov 20 '12 at 18:23
    
The callback for do_settings_fields() can include that test. –  toscho Nov 20 '12 at 18:26
    
Ooooh, that would be nicely elegant! Though, it might work even better in the add_settings_field() callback. –  Chip Bennett Nov 20 '12 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 '12 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 '12 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.

share|improve this answer
    
It seems like this solution wouldn't output any editable fields for non admin users, amirite? –  jessica Nov 20 '12 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 '12 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. –  Chip Bennett Nov 20 '12 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 '12 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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