Using WordPress ‘is_site_admin()’ PHP function

The is_site_admin() WordPress PHP function determines if a user is a site admin.

Usage

$is_admin = is_site_admin( 'example_user' );

Parameters

  • $user_login (string, optional) – Username for the user to check. Default: ''

More information

See WordPress Developer Resources: is_site_admin()

Note: Plugins should use is_multisite() instead of checking if this function exists to determine if multisite is enabled. This function must reside in a file included only if is_multisite() is true due to legacy function_exists() checks to determine if multisite is enabled. See also is_super_admin().

Examples

Check if a user is a site admin

Check if the given user is a site admin and display a message accordingly.

$user_login = 'example_user';
if ( is_site_admin( $user_login ) ) {
    echo 'The user is a site admin.';
} else {
    echo 'The user is not a site admin.';
}

Display site admin menu

Display a site admin menu if the user is a site admin.

function display_admin_menu( $user_login ) {
    if ( is_site_admin( $user_login ) ) {
        echo '**Admin Menu**';
    }
}
display_admin_menu( 'example_user' );

Restrict access to a page

Restrict access to a page for non-site-admin users.

function restrict_access( $user_login ) {
    if ( !is_site_admin( $user_login ) ) {
        die( 'Access denied. Only site admins can access this page.' );
    }
}
restrict_access( 'example_user' );

Get a list of site admins

Get a list of all users and display the usernames of the site admins.

$all_users = get_users();
foreach ( $all_users as $user ) {
    if ( is_site_admin( $user->user_login ) ) {
        echo 'Site admin: ' . $user->user_login . '<br>';
    }
}

Add a site admin role to a user

Add the site admin role to a user if they are not already a site admin.

function add_site_admin( $user_login ) {
    if ( !is_site_admin( $user_login ) ) {
        $user = get_user_by( 'login', $user_login );
        $user->add_role( 'administrator' );
        echo 'User is now a site admin.';
    } else {
        echo 'User is already a site admin.';
    }
}
add_site_admin( 'example_user' );