Show member role within profile

Hi guys, I'd like to be able to show my membership 'roles' as visible as possible on my member's profiles, similar to how WPMUDEV does it.

My membership levels are name slightly differently to the standard Wordpress role so that would need to be taken into account.

Subscriber - Compass
Member - Navigator / NavigatorPlus
Editor - Staff
Admin - Staff

Any ideas?

  • Patrick
    • Support Monkey

    Hiya @bentemple

    Something like this should get you going (you could add it to member-header.php for example):

    <?php
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    
    if ($user_role == 'administrator') {
    echo 'Administrator';
    } elseif ($user_role == 'editor') {
    echo 'Editor';
    } elseif ($user_role == 'author') {
    echo 'Author';
    } elseif ($user_role == 'contributor') {
    echo 'Contributor';
    } elseif ($user_role == 'subscriber') {
    echo 'Subscriber';
    } else {
    echo '<strong>' . $user_role . '</strong>';
    }
    ?>

    Snippet from the first result in this search:
    http://wordpress.org/search/display+role

  • Paul Barthmaier
    • Code Wrangler

    Hey Ben,

    I would have thought there'd be a plugin that could handle what you're asking about, but I don't see anything that's still being maintained right now. I did find this string which might work for you:

    http://wordpress.org/support/topic/display-user-role-on-profile-page

    At the bottom there is some php that might be good for you, as in:

    <?php
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    
    if ($user_role == 'administrator') { echo 'Admin';}
      elseif ($user_role == 'editor') { echo 'Staff';}
      elseif ($user_role == 'author') {echo 'NavigatorPlus';}
      elseif ($user_role == 'contributor') { echo 'Navigator'; }
      elseif ($user_role == 'subscriber') { echo 'Compass'; }
      else { echo '<strong>' . $user_role . '</strong>';
    }
    ?>
  • bentemple
    • Site Builder, Child of Zeus

    Hi guys, I found the member-header.php file within my theme's folder and tried both snippets of code, with and without the opening/closing php statement and it only showed the code itself within the member's header area.

    I feel like that is definitely the right file to edit (although should I have a child member-header.php file and how do i create that?) however, not exactly sure where to add the code on that file (sadly I'm not much of a coder, a plugin would be great for this, but as you say Pali, there doesn't seem to be one currently!)

  • bentemple
    • Site Builder, Child of Zeus

    Thanks Pali, I've copied the file into the Child theme and I can see that's affecting the site as it should, that's a technique I can easily replicate for other challenges!

    Although I can't get either of the code snippets to work, I either get an error message by placing the entire code at the top or bottom of the file (see screenshot).

    Or I can just see the code if I place it under <!-- #item-header-avatar --> (see screenshot).

    A bit confused where to place the code and whether to include the opening and closing PHP statements if placed within the code (i.e. <?php and ?>:wink:.

    It would be really, really, really helpful if you could show me the entire file member-header.php with the new code placed inside :slight_smile:

  • Tom Eagles
    • Syntax Hero

    Hiya,
    Greetings of the day.
    I was doing a regular followup today and found that we missed your last post on this one.
    Are you still seeking support on this one or managed to get it resolved ? Please let us know so that we can take it further and assist you on the same
    Thanks for being a great community member !
    Cheers
    Tom

  • Patrick
    • Support Monkey

    Hiya @bentemple

    To get things to display properly, you'll want to add the code directly between where you see the following in member-header.php

    <div id="item-header-content">
    ...code here...
    	<h2>
    		<a href="<?php bp_displayed_user_link(); ?>"><?php bp_displayed_user_fullname(); ?></a>
    	</h2>

    So the file content would look like this:

    <div id="item-header-content">
    
    <?php
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    
    if ($user_role == 'administrator') {
    echo 'Staff';
    } elseif ($user_role == 'editor') {
    echo 'Staff';
    } elseif ($user_role == 'member') {
    echo 'Navigator / NavigatorPlus';
    } elseif ($user_role == 'subscriber') {
    echo 'Compass';
    } else {
    echo 'Network Admin';
    }
    ?>
    
    	<h2>
    		<a href="<?php bp_displayed_user_link(); ?>"><?php bp_displayed_user_fullname(); ?></a>
    	</h2>

    Let us know if this helps :slight_smile:

  • Patrick
    • Support Monkey

    @bentemple

    You are adding the code to member-header.php in your theme, right?

    Let's do a little test to be sure something can be displayed. Right after this in that template...
    <div id="item-header-content">
    ...insert some test text. Maybe something as simple as...
    This is some test text
    Then upload the file to your site and refresh any member page. If that text displays, then there is likely something corrupted in the code you pasted in earlier.

  • Patrick
    • Support Monkey

    Hi again @bentemple

    Did you try the test I proposed above? Add just a bit of text directly beneath the following in member-header.php
    <div id="item-header-content">

    Then upload and refresh a member page to see if your text displays. If it does, then we'll look again at the code.

  • Patrick
    • Support Monkey

    Hi @bentemple

    I'm doing a bit of housekeeping by going through some older threads, and I noticed this one is still open.

    Just to get it out of our active ticket system, I'm going to mark it resolved.

    But if you still need assistance on this issue, please don't hesitate to re-open it by checking the "Mark as not resolved" box below.

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.