As @butlerblog reply stated, you should not use current_user_can to check against a role
This notice is specifically added in the PHP documentation of has_cap
function which is called by current_user_can
While checking against a role in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.
The CORRECT way to do this is to get the user and check the $user->roles
, like this:
if( ! function_exists( 'current_user_has_role' ) ){
function current_user_has_role( $role ) {
$user = get_userdata( get_current_user_id() );
if( ! $user || ! $user->roles ){
return false;
}
if( is_array( $role ) ){
return array_intersect( $role, (array) $user->roles ) ? true : false;
}
return in_array( $role, (array) $user->roles );
}
}
Here's some helper functions I use to do this (as sometimes i don't want just current user):
if( ! function_exists( 'current_user_has_role' ) ){
function current_user_has_role( $role ){
return user_has_role_by_user_id( get_current_user_id(), $role );
}
}
if( ! function_exists( 'get_user_roles_by_user_id' ) ){
function get_user_roles_by_user_id( $user_id ) {
$user = get_userdata( $user_id );
return empty( $user ) ? array() : $user->roles;
}
}
if( ! function_exists( 'user_has_role_by_user_id' ) ){
function user_has_role_by_user_id( $user_id, $role ) {
$user_roles = get_user_roles_by_user_id( $user_id );
if( is_array( $role ) ){
return array_intersect( $role, $user_roles ) ? true : false;
}
return in_array( $role, $user_roles );
}
}
Then you can just do this:
current_user_has_role( 'editor' );
or
current_user_has_role( array( 'editor', 'administrator' ) );
if( current_user_can('editor') || current_user_can('administrator') )