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 admin or not:
if(current_user_can('administrator')){
//current user is an administrator
}
written by Mehdi Nazari about in WordPress WordPress Plugins WordPress Themes