How to check if the current user is Administrator in WordPress
Last updated:
Here's how to check if the currently logged in user is an administrator in WordPress. This is useful if you want your content to be displayed only to administrators or to non-administrators.
How to check whether a user is administrator or not
To check if the currently logged in user is an administrator, use the current_user_can function.
To check if a user is an administrator, you can specify the capability as an argument of current_user_can function (e.g. manage_options).
For Administrators
if ( current_user_can( 'manage_options' ) ) {
//Something in the case of admin
}
For Non-Administrators
if ( !current_user_can( 'manage_options' ) ) {
//Something other than an administrator
}
Conditional branching between administrators and non-administrators
if ( current_user_can( 'manage_options' ) ) {
//Something in the case of admin
} else {
//Something other than an administrator
}
Avoid using the current_user_can function with a Role (administrator) as an parameter. It may return unintended results.
The usage of the current_user_can function is described in the following article.
How to use the current_user_can function in WordPress
Create Custom function
To create a Custom function to check if a user is an administrator, add the following code to functions.php in the theme.
function is_admin_user() {
return current_user_can( 'manage_options' );
}
The following Custom functions are used
if( is_admin_user() ) {
//Something in the case of admin
} else {
//Something other than an administrator
}
Common Use Cases
Check to see if you are an administrator in the following cases
- To show debug information to the administrator only
- To show the design for testing to administrators only
- Only administrator does not want to show ads