Get user role by ID WordPress

wordpress display user role
wordpress get user id
wordpress check user role function
wordpress check if user is role
wordpress get user meta
wordpress user roles
wordpress get admin id
check if role exists wordpress

I need to somehow check someone's role with only their id. I have found the current_user_can() check. But this only works for people that are logged in. How would I check for this if that user isn't the current user? I am using a phone order system but that uses the admin/specific account to order for other people.

You can't get user role directly. First, you have to get the user_meta_data, and it will return an Object that will contain user roles.



$user_roles=$user_meta->roles; //array of roles the user is part of.

How to get role of user, Use the user ID and WP_User : $user = new WP_User( $user_id ); print wp_sprintf_l( '%l', $user->roles );. Update. /** * Get user roles by user ID. * * @​param int  You can't get user role by ID directly. You can get all the roles a user is assigned to. Let's get all the roles and check if the role you're interested in is there or now.

get_userdata() | Function, There's not actually a WordPress function to give you the role of a user so it's necessary to concoct your own. This is what I used for WooCommerce Members​  Get Current User’s Role in WordPress. This function is pretty versatile since if you pass a specific user’s ID or a WP_User object to it, it will return that user’s role. If you pass nothing to it, you’ll get the role of the current user instead.

$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;

if ( in_array( 'administrator', $user_roles, true ) ) {
    //echo User is a administrator';

Get current user role in WordPress, We also show you how to get the WordPress user role with a SQL statement at the bottom of the page. If you have the WordPress user's id, the get_userdata  Creates WordPress network meta and sets the default values. Returns the object subtype for a given object ID of a specific type. Create and log a user request to perform a specific action. Finds and exports personal data associated with an email address from the user and user_meta table.

hello try this optimal code

if (get_userdata($post->post_author)->roles[0] == 'your_specified_role'){
   echo 'role exist';
   echo 'role does not exist';

Get User Role WordPress, While working on a membership site I wanted to retrieve a user's role by their user id, but I couldn't find a quick function to do this. WordPress  Get current user role in WordPress. User roles and capabilities in WordPress are something I’ve been looking at recently for the Discussion Board and WooCommerce Members Only plugins. One thing I need to check is the user’s role and whether that grants them permission to view certain content.

Check WordPress User Role By ID – Peter R Knight, You could expand it and check if a user has a given role or similar. PHP code to get a users role and to list all Wordpress roles. PHP. $user_info = get_userdata(​2152); implode(', ', $user_info->roles) . "\n";. echo 'User ID: ' . daniyalahmedk’s example is redundant. The code below is the exact same thing but with less overhead. Get current user role by ID : There is no need to invoke a new WP_User object. Get current user role by ID : Before the plugins_loaded action it returns 0. This function is not means for retrieving the current logged in user as it been made

How to get a users Roles and how to list all Wordpress roles in PHP , Here is a plug-n-play function to check user roles, based on an array of Also, if you want to check a specific user ID, you can use the second  The results will be all users whose user names, IDs, or emails that start with “jo”. The * can be placed before or after your search query. When placed before, the results will be all users that end in your query.

WordPress Check User Roles, Find answers, share tips, and get help from other WordPress experts. Join now (​it's free)!. Youtube cover image  blog_id - The current blog's ID, unless multisite is enabled and another ID is provided. role - Limit the returned users to the role specified. Roles_and_Capabilities#User_Levels. role__in - Limit the returned users that have one of the specified roles.

  • Like described, I use a special phone order system. Orders are sometimes placed for a specific user by (for example) admin. And when this happens I need to check the role of that original user
  • $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("subscriber", $user_roles)){} Results in a role check for subscriber.
  • Your answer looks an awful lot like Ahmad's answer of 3 months ago. Does this add anything new?
  • A user can have multiple roles in WordPress, so the correct way to assert is in_array