Display Staff for certain user roles

Hello,

Could you please help me write some code to display a Staff label if the user is an Editor or Administrator? This is what I came up with, will it work?

if (current_user_can('edit_posts') ) {
echo "Staff";
}

This code will be put in the Member Header template for BuddyPress.

Thanks,

Alex :smiley:

  • Sajid
    • DEV MAN’s Sidekick

    Hi Alex S,

    Hope you are doing good today :slight_smile:

    You are on right direction. To add the custom label in bbPress topic you need to customize the bbPress templates. Here is a step by step guide to edit the bbPress templates.

    https://codex.bbpress.org/themes/theme-compatibility/step-by-step-guide-to-creating-a-custom-bbpress-theme/

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:

    Cheers, Sajid

  • Panos
    • SLS

    Hello Alex S ,

    It depends where you wish to display the “Staff” label.

    If you would like it at the date row, you can insert your code in following file wp-content/plugins/bbpress/templates/default/bbpress/loop-single-reply.php

    One note here, it is recommended that you copy that file at your child theme: wp-content/themes/you-(child)theme/bbpress/loop-single-reply.php, so your changes will not be removed after bbpress update.

    In case you prefer to display that label under the user name, you can simply add the following snipped at your (child)theme’s functions.php

    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo "Staff";
    }
    }

    Hope this helps!

    Kind regards,

    Panos

  • Alex Stine
    • System Administrator

    Hello,

    I am using this code in functions.php.

    /*Display staff label on BBPress*/
    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo"Staff";
    }
    }

    The problem is I cannot figure out how to add a div to style the label with CSS. Could you please add a div to the code and repost it here for me?

    Thanks,

    Alex :smiley:

  • Panos
    • SLS

    Sure Alex,

    Say use a class “bbpress-staff-label”:

    /*Display staff label on BBPress*/
    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo '<div class="bbpress-staff-label">Staff</div>';
    }
    }

    Then you can use .bbpress-staff-label{ color: #555; font-style…etc } in your css to style it :slight_smile:

    Cheers!

    Panos

  • Alex Stine
    • System Administrator

    Hello,

    I would like to display the Staff label instead of the translated BBPress role, here is my code, unfortunately, it through a syntax error so I am posting here. Could you please help me modify it?

    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo '<div class="bbpress-staff-label">Staff</div>';
    } else {
    function get_user_role_redux($user_id) {
    global $wp_roles;
    $roles = array();
    $user = new WP_User( $user_id );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )
    $roles[] .= translate_user_role($wp_roles->roles[$role]['name']);
    }
    return $roles[0];
    }

    Thanks,

    Alex :smiley:

  • Panos
    • SLS

    Hello alex,

    Could you please try out this code:

    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo '<div class="bbpress-staff-label">Staff</div>';
    } else {
    global $current_user;
    echo '<div class="bbpress-userrole-label-wrap">' . get_user_role_redux($current_user->ID) . '</div>';
    }
    }

    function get_user_role_redux($user_id) {
    global $wp_roles;

    $roles = '';
    $user = new WP_User( $user_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){
    $roles .= '<span class="bbpress-userrole-label">' . translate_user_role($wp_roles->roles[$role]['name']) . '</span>';
    }

    }
    return $roles;

    }

    and let us know how it works for you?

    Cheers!

    Panos

  • Alex Stine
    • System Administrator

    Hello,

    Now the default BBPress role is showing next to the translated WordPress role, can you please adjust the code to fix that? I could remove with CSS, the problem is I do not want to reposition the roles for mobile, so it would be easier to fix the code above.

    Thanks,

    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello,

    I modified the code to look like this.

    /*Display staff and user role label on BBPress*/
    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo '<div class="bbp-staff-role">Staff</div>';
    } else {
    global $current_user;
    echo '<div class="bbp-user-role">' . get_user_role_redux($current_user->ID) . '</div>';
    }
    }

    function get_user_role_redux($user_id) {
    global $wp_roles;

    $roles = '';
    $user = new WP_User( $user_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){
    $roles .= '<span class="bbp-user-role">' . translate_user_role($wp_roles->roles[$role]['name']) . '</span>';
    }

    }
    return $roles[0];

    }

    The problem is where the role should be displayed, there is a > like it is out of place or something. Could you please check the code?

    Thanks,

    Alex :smiley:

  • Sajid
    • DEV MAN’s Sidekick

    Hi Alex S,

    Hope you are doing good today :slight_smile:

    The code you posted above does not seem to cause this issue. There is no missing less than sign and more over its being printed in “bbp-author-role” class instead of “bbp-user-role”.

    So, you should check where you have added the code that is printing “bbp-author-role”.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Alex Stine
    • System Administrator

    Hello,

    I am not sure where the > came from, so I came up with this solution.

    CSS:

    .bbp-author-role {
    display: none;
    }
    .bbp-role-label {
    font-size: 16px;
    font-weight: bold;
    color: white;
    background-color: black;
    text-align: center;
    padding: 4px;
    margin-top: 15px;
    display: table;
    }
    .bbp-staff-label {
    font-size: 16px;
    font-weight: bold;
    color: white;
    background-color: blue;
    text-align: center;
    padding: 4px;
    margin-top: 15px;
    display: table;
    }

    functions.php:

    /*Display staff and user role label on BBPress*/
    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    if (current_user_can('edit_posts') ) {
    echo '<div class="bbp-staff-label">Staff</div>';
    } else {
    global $current_user;
    echo '<div class="bbp-role-label">' . get_user_role_redux($current_user->ID) . '</div>';
    }
    }

    function get_user_role_redux($user_id) {
    global $wp_roles;

    $roles = '';
    $user = new WP_User( $user_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){
    $roles .= '<span class="bbp-role-label">' . translate_user_role($wp_roles->roles[$role]['name']) . '</span>';
    }

    }
    return $roles[0];

    }

    This ticket can now be marked resolved, thanks to everyone that was involved here.

    Thanks,

    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello,

    Well, not exactly fixed. The role is properly assigned, but users cannot view the roles if they are signed out or do not have the edit_posts capability. Can you please help me modify the code so that anyone can still view the role?

    Thanks,

    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello,

    I just found another big problem in my code. Staff is being displayed for all users, but it can only be seen by users that have the edit_post capability. What needs to happen is users should only be tagged Staff if they have edit_posts as a capability.

    Thanks,

    Alex :smiley:

  • Panos
    • SLS

    Hi Alex S ,

    Got it :slight_smile:

    Try replacing all previous code with this one:

    add_action ( 'bbp_theme_before_reply_author_admin_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    global $post, $wp_roles;

    $author_id = bbp_get_topic_author_id( $post->ID );

    $roles = array();
    $user = new WP_User( $author_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){

    $roles[] = $role;
    }
    }

    if( in_array( 'administrator', $roles ) || in_array( 'editor', $roles ) ){
    echo '<div class="bbpress-staff-label">Staff</div>';
    }
    else{

    foreach( $roles as $key => $r ){
    echo '<div class="bbpress-userrole-label-wrap">' . $r . '</div>';
    }

    }

    }

    Cheers!

    Panos

  • Alex Stine
    • System Administrator

    Hello panoskatws ,

    Here is an updated topic link so you can look, deleted the last topic without thinking.

    http://yourtechadvisors.com/forums/topic/april-editing/

    Also, I did not want to me a lazy member, I looked in to this a little more and can provide you with this error message.

    Fatal error: Call to undefined function get_user_role_redux() in /public_html/wp-content/themes/wpex-thoughts/bbpress/loop-single-reply.php on line 53

    I have removed the code at this time so things will go back to normal.

    For future code, please use these updated classes.

    .bbp-staff-label
    .bbp-role-label

    Please advise.

    Thanks,

    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello panoskatws ,

    I just found the cause in a modified BBPress template. It is all fixed up now.

    The last remaining problem can be found here.

    http://yourtechadvisors.com/forums/topic/testing/#post-5787

    As you can see, the role for this user is

    subscriber

    bbp_participant.

    The Subscriber role was actually renamed to Basic Member, why isn’t it showing Basic Member? Also, why is the BBPress role there?

    EDIT: The roles are still not showing up if you are not logged in as Editor or Admin.

    Thanks,

    Alex :smiley:

  • Panos
    • SLS

    Hi Alex,

    I’d need to connect with ftp to check out the code you are using now but I would need your permission for this first.

    Also I would need your help to guide me in which file (probably theme’s functions.php but can’t be sure ) you have entered your code. I believe I can find your ftp credentials so no need too resend them.

    Please confirm that you have kept a backup before I connect :slight_smile:

    Thanks!

    Panos

  • Panos
    • SLS

    Hi Alex S ,

    EDIT: The roles are still not showing up if you are not logged in as Editor or Admin.

    This is because if the action hookbbp_theme_before_reply_author_admin_details we used from the beginning as I wrongfully had the impression that you meant to show this for admins/editors.

    I have replaced it with bbp_theme_after_reply_author_details and author role should be now visible to subscribers and logged out visitors.

    Regarding the role name Basic Member I have updated the code and it should be displaying properly now :slight_smile:

    Cheers!

    Panos

  • Alex Stine
    • System Administrator

    Hello panoskatws ,

    It still shows Participant, all it should show is the WordPress role. Could you please adjust the code so that the BBPress role is not showing at all?

    Full code:

    add_action ( 'bbp_theme_after_reply_author_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    global $post, $wp_roles;

    $wpr = $wp_roles->roles;

    $author_id = bbp_get_topic_author_id( $post->ID );

    $roles = array();
    $user = new WP_User( $author_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){

    $roles[] = $role;
    }
    }

    if( in_array( 'administrator', $roles ) || in_array( 'editor', $roles ) ){
    echo '<div class="bbp-staff-label">Staff</div>';
    }
    else{

    foreach( $roles as $key => $r ){
    $role_name = isset( $wpr[$r]['name'] ) ? $wpr[$r]['name'] : $r;

    echo '<div class="bbp-role-label">' . $role_name . '</div>';
    }

    }

    }

    Thanks,

    Alex :smiley:

  • Panos
    • SLS

    Hey there Alex,

    I simply removed bbpress roles from the output, so here is how the final code looks like now at the bottom of your functions.php file:

    add_action ( 'bbp_theme_after_reply_author_details', 'wpmu_staff_label' );

    function wpmu_staff_label(){

    global $post, $wp_roles;

    $wpr = $wp_roles->roles;

    $author_id = bbp_get_topic_author_id( $post->ID );

    $roles = array();
    $user = new WP_User( $author_id );

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

    foreach ( $user->roles as $role ){

    if( $role != 'bbp_keymaster' &&
    $role != 'bbp_spectator' &&
    $role != 'bbp_blocked' &&
    $role != 'bbp_moderator' &&
    $role != 'bbp_participant'
    )

    $roles[] = $role;
    }
    }

    if( in_array( 'administrator', $roles ) || in_array( 'editor', $roles ) ){
    echo '<div class="bbp-staff-label">Staff</div>';
    }
    else{

    foreach( $roles as $key => $r ){
    $role_name = isset( $wpr[$r]['name'] ) ? $wpr[$r]['name'] : $r;

    echo '<div class="bbp-role-label">' . $role_name . '</div>';
    }

    }

    }

    Let me know how this works :slight_smile:

    Note:

    I would recommend to start using a child theme, as sooner or later you will have to update the theme and then you will need to redo all these moderations.

    Cheers!

    Panos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.