Post Reads 33 reads

Get logged in user assigned roles. Use function  wp_get_current_user() to get the current/logged in user info.

Output: Output:

print_r( prefix_get_current_user_roles() );

// Output:
// Array
// (
//     [0] => administrator
// )

Code: Code:

<?php
if( ! function_exists( 'prefix_get_current_user_roles' ) ) :
/**
* Get current user roles
*
* E.g. print_r( prefix_get_current_user_roles() );
*
* Output:
*
* Array
* (
* [0] => administrator
* )
*
* @todo Change `prefix` with your own unique slug.
*
* @since 1.0.0
* @return mixed User roles.
*/
function prefix_get_current_user_roles()
{
// Not logged in? Return false.
if( ! is_user_logged_in() ) {
return false;
}
// Logged in? Return all roles.
return wp_get_current_user()->roles;
}
endif;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: