Skip to content

Instantly share code, notes, and snippets.

@wpbullet
Forked from kellenmace/get-user-role.php
Last active July 27, 2017 03:09
Show Gist options
  • Save wpbullet/60d40b24473b8a600b59769370c42c6d to your computer and use it in GitHub Desktop.
Save wpbullet/60d40b24473b8a600b59769370c42c6d to your computer and use it in GitHub Desktop.
Get user role in WordPress
<?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