Skip Navigation

[Resolved] show User Role in the Loop of a View of Users

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View of Users, and I would like to display the role of each User in the Loop.

Solution:
Add the following custom shortcode to your functions.php file:

function get_user_role_func( $atts )
{
  $a = shortcode_atts( array(
      'userid' => ''
  ), $atts );
  $userdata = get_user_by('ID', $a['userid']);
  $user_roles = $userdata->roles;
  $user_role = array_shift($user_roles);
  return $user_role;
}
add_shortcode( 'get_user_role', 'get_user_role_func' );

Then in your View, use the shortcode in a conditional like this:

[wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'editor' )"]
I am Editor
[/wpv-conditional]
[wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'administrator' )"]
I am Admin
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Nashaat 3 years, 11 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#952839

I have created a view that show all users and i am trying to show the user Role in user listing.
i tried to add current user but this wont work because its specified for logged in user only.

[wpv-conditional if="( '[wpv-current-user info='id']' eq '1' ) AND ( '[wpv-current-user info='role']' eq 'administrator' )"]Administrator[/wpv-conditional]

what is the best way to show the role of user in views?

#953150

Hi, there's not a built-in way to get the User's role like this, but another ticket here on the forums discussed a strategy using a custom shortcode and conditionals:
https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/

Let me know if this will not solve the problem for you.

#953222

I have followed the link you have provided. seems not to work.

I have added this function:

function get_author_role()
{
    global $authordata;
   
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
    return $author_role;
}  
add_shortcode( 'user_role_func', 'get_author_role' );

then i have registered user_role_func in third party showrtcodes

after that i have added following code to the Users views Loop

[wpv-conditional if="( '[user_role_func]' eq 'editor' )"]
I am Editor
[/wpv-conditional]

so after testing it just work when role = administrator. Other roles like Editor or anything else doesn't show the conditional display content.

PS: what i need is to show the role of every user in the users loop... this code show the current user role only in all of users loop...

#953233

I should have been more specific, sorry. That code needs a few adjustments for this case. Here's the edited code that you should use:

function get_user_role_func( $atts )
{
  $a = shortcode_atts( array(
      'userid' => ''
  ), $atts );
  $userdata = get_user_by('ID', $a['userid']);
  $user_roles = $userdata->roles;
  $user_role = array_shift($user_roles);
  return $user_role;
}
add_shortcode( 'get_user_role', 'get_user_role_func' );

Then in your View, use the shortcode in a conditional like this:

[wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'editor' )"]
I am Editor
[/wpv-conditional]
[wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'administrator' )"]
I am Admin
[/wpv-conditional]
#953234

Also - be sure to register "get_user_role" in Toolset > Settings > Frontend Content > Third party shortcode arguments.

#953239

AWESOME!! this solved the problem. I will test this little further tomorrow and if no issues i will close this ticket. Thanks!

#953539

Awesome everything works as expected! Thank you Chris