How to display some settings for super admin user only using Settings API

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?

Solutions Collecting From Web of "How to display some settings for super admin user only using Settings API"

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.

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.