Last active
February 23, 2018 07:41
-
-
Save maheshwaghmare/93ad3fd838734a86ccd0fe9bcee2f6e7 to your computer and use it in GitHub Desktop.
Check logged in user role in WordPress
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 | |
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