Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active February 23, 2018 07:41
Show Gist options
  • Save maheshwaghmare/93ad3fd838734a86ccd0fe9bcee2f6e7 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/93ad3fd838734a86ccd0fe9bcee2f6e7 to your computer and use it in GitHub Desktop.
Check logged in user role in WordPress
<?php
if( ! function_exists( 'prefix_user_has_role' ) ) :
/**
* Check logged in user has role
*
* @todo Change `prefix` with your own unique slug.
*
* E.g. var_dump( prefix_user_has_role( 'administrator' ) );
*
* Output:
*
* bool(true)
*
* @since 1.0.0
* @param string $role User role.
* @return boolean User role status.
*/
function prefix_user_has_role( $role = '' )
{
// Not logged in? Return false.
if( ! is_user_logged_in() ) {
return false;
}
// Role exist? Return true.
if ( in_array( $role, (array) wp_get_current_user()->roles ) ) {
return true;
}
// Default. Return false.
return false;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment