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
Most Popular Articles
- What’s new in WordPress 5.6? New Features and Changes
- How to Add width/height to Images in WordPress
- What’s New in WordPress 5.5.3? Fixed 1 Bug
- WordPress 5.6 jQuery version updates and solution for jQuery
- WordPress Version List (Release Date/Code Name)
- What’s new in WordPress 5.5? New Features and Changes
- What’s New in WordPress 5.5.2? Fixed 10 security issues
- How to check if the current user is Administrator in WordPress
- Vulnerability of File Manager WordPress plugin and Solutions
- Recommend Premium Themes for WordPress (By Usage)
New Articles
- WordPress 5.6 jQuery version updates and solution for jQuery
- What’s New in WordPress 5.5.3? Fixed 1 Bug
- What’s New in WordPress 5.5.2? Fixed 10 security issues
- What’s new in WordPress 5.6? New Features and Changes
- How to use the current_user_can function in WordPress
- How to check if the current user is Administrator in WordPress
- WordPress Template Tags (162 Types)
- How to Setup Cloudflare for WordPress
- Vulnerability of File Manager WordPress plugin and Solutions
- What is the WordPress Template Hierarchy? Priorities by Page Type
- What’s new in WordPress 5.5.1? 44 bugs fixed
- What is a WordPress child theme? Tutorial to create a child theme
- Recommend Premium Themes for WordPress (By Usage)
- Best Free Themes for WordPress (By Usage)
- What is WordPress Theme? Summary of how to choose and use a theme
- What’s new in WordPress 5.5? New Features and Changes
- How to Add width/height to Images in WordPress
- How to Install WordPress
- How to install WordPress locally
- How to Install WordPress Manually