-
-
Save wpbullet/60d40b24473b8a600b59769370c42c6d to your computer and use it in GitHub Desktop.
Get 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 | |
/* | |
* Get user's role | |
* | |
* If $user parameter is not provided, returns the current user's role. | |
* Only returns the user's first role, even if they have more than one. | |
* Returns false on failure. | |
* | |
* @param mixed $user User ID or object. | |
* @return string|bool The User's role, or false on failure. | |
*/ | |
function km_get_user_role( $user = null ) { | |
$user = $user ? new WP_User( $user ) : wp_get_current_user(); | |
return $user->roles ? $user->roles[0] : false; | |
} | |
//Disallow direct access | |
defined( 'ABSPATH' ) or die( 'No Access' ); | |
add_filter( 'wp_headers', 'remove_headers', 10000 ); | |
function remove_headers( $headers ) { | |
unset($headers['Pragma']); | |
unset($headers['Expires']); | |
unset($headers['Cache-Control']); | |
$headers['User-Role'] = km_get_user_role(); | |
return $headers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment