Skip to content

Instantly share code, notes, and snippets.

@banderon
Last active December 27, 2017 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banderon/11269078 to your computer and use it in GitHub Desktop.
Save banderon/11269078 to your computer and use it in GitHub Desktop.
wordpress user has role check
<?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