Last active
December 27, 2017 15:21
-
-
Save banderon/11269078 to your computer and use it in GitHub Desktop.
wordpress user has role check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if a role is assigned to a particular user | |
* | |
* @param string $role | |
* @param int $user_id | |
* @return bool | |
*/ | |
function user_has_role( $role, $user_id = null ) { | |
if ( is_numeric( $user_id ) ) { | |
$user = get_userdata( $user_id ); | |
} | |
else { | |
$user = wp_get_current_user(); | |
} | |
if ( ! empty( $user ) ) { | |
return in_array( $role, (array) $user->roles ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment