• Resolved pacfire

    (@pacfire)


    Hi everyone,

    I have a really basic question – I’m sorry.

    I would like to display a Users, user role on the front end (different user roles have different permissions and capabilities – if they can see their role they will know if they need a promotion to access more features).

    I do not have the faintest idea on how to do this? Running a very basic Storefront Child theme and plan on putting the info on the “my account” page at this stage.

    Sorry for the question – love this plugin and always appreciate the help from forum members.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @pacfire

    Thank you for your kind words. We appreciate it.

    If you want to display current user roles, you can use the code snippet from this article: https://njengah.com/get-current-user-role-in-wordpress/

    Hopefully, that helps.

    Thread Starter pacfire

    (@pacfire)

    Hi @caseproof

    Thank you so much for the reply.

    I feel like this is going to make me look pretty stupid, but I can see it forming an important part of the site, so I’m going to ask anyway.

    I found that snippet early on – but I have no idea how to use it in the premise of bringing the user role name forward and putting it somewhere on the site.

    Basically – different user levels have different access levels on the site we are developing. If a user doesn’t have access to a particular part of the site – it can be explained by their access level.

    Right now, I have a widget in the sidebar called “logged in user” under which, with shortcode from Shortcodes Ultimate, I have displayed their name, username, payroll number (a custom field) and I have tried to drag role forward also using the “role” as meta, but it doesn’t work so I’m doing something very wrong.

    I’m sorry about this follow up question – I’m not a developer by any means, I can occasionally change code to suit our situation, but in some circumstances, like this, I have no idea what I’m looking at…

    My apologies once again – thank you for your help thus far.

    Amanda @ Pac Fire

    Plugin Author Caseproof

    (@caseproof)

    No need to apologize. To display all roles for active user, you can use this code and add it at the end of your theme’s functions.php file:

    add_shortcode('mb-user-role', function() {
      global $wp_roles;
      if( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        echo '<ul>';
        foreach( $roles as $role ){
          echo '<li>' . $wp_roles->roles[ $role ]['name'] . '</li>';
        }
        echo '</ul>';
      }
    });

    and use it as a shortcode [mp-user-role].

    Hopefully, that helps.

    Thread Starter pacfire

    (@pacfire)

    Hi @caseproof,

    Thank you so much for your time with this. I put this in the functions PHP of our theme and then used the shortcode – it didn’t return any results, but that’s because I’m likely doing something wrong here.

    I did develop a workaround – based on user access and visibility based on [members_logged_in role=“role”] – and it actually has worked out better as we’re able to provide more information on the users role when (what they can do).

    So I just wanted to thank you – because without you I doubt we would have come up with this solution.

    @caseproof, just wanted to point out that you have a typo either in your code or in the shortcode name that you suggested: the name you registered in the code is 'mb-user-role', however you suggested adding it as [mp-user-role] (notice the difference in the second letter).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display User Role on Front End’ is closed to new replies.