User authentication is essential while developing WordPress. Detecting if the current user is an administrator in WordPress is not challenging, using the current_user_can function. This function is one of the most important security functions in WordPress.
current_user_can
function: Check if the current user has a specified ability
The current_user_can
function is responsible for checking if the current user has a specified ability or not.
We can use this function to check if the current user is an administrator or not.
current_user_can
function syntax
WordPress current_user_can
function needs two parameters to work as you need.
it just checks if the current user can do something with something or not!
current_user_can($capability, $object_id, ...$moreArgs)
$capability
determines what ability you want to check.
$object_id
determines what is the ID of the object you need the ability to be checked for (optional).
…$moreArgs
in syntax means that you can define more arguments based on the function usage.
How to use current_user_can
function to Check if the current user is an Admin
If you check the administrator capability for the current user, you can determine if the current user is an admin or not:
if(current_user_can('administrator')){
//current user is an administrator
}
DO NOT Use is_admin()
to Detect an Administrator User
is_admin()
is the function to check if the user is currently in the admin panel; it does not work on the frontend of the WordPress.
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Check if the Current User is Administrator in WP"?