0

I'm working on a custom theme. For my comment section, if the user role is administrator I want to display "Admin", if subscriber "Subscriber", etc.

The problem is, I will add this code and "Admin" shows beside all users even if not admin (I've tried changing the role, this is just an admin example):

if ( current_user_can( 'administrator' ) ) {
    echo '<div class="admin-tag"></div>';
}

Here is another one I tried, did not work at all:

if( current_user_can( 'administrator' ) ) {
  echo 'Admin';
} else if ( current_user_can( 'subscriber' ) ) {
  echo 'Subscriber';
}
2

4 Answers 4

4

You are passing a role name, administrator, to current_user_can. Looking at the Codex page this is supported but not guaranteed to work, and should generally be avoided:

Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly (see #22624).

Instead, you should use a capability:

if ( current_user_can( 'manage_options' ) ) {
    echo 'Admin';
} else if ( current_user_can( 'edit_pages' ) ) {
    echo 'Editor';
} else if ( current_user_can( 'publish_posts' ) ) {
    echo 'Author';
} else if ( current_user_can( 'read' ) ) {
    echo 'Subscriber';
}

You can find a list of capabilities and roles types on the Codex as well.

Updated This addresses the case of looping over the users to display each user's role:

foreach ( $comment_users as $user_id ) {
    wp23234_show_user_role( $user_id );
}

function wp23234_show_user_role( $user_id ) {
    $data  = get_userdata( $user_id );
    $roles = $data->roles;

    if ( in_array( $roles, 'administrator' ) ) {
        echo 'Administrator';
    } else if ( in_array( $roles, 'editor' ) ) {
        echo 'Editor';
    } else if ( in_array( $roles, 'author' ) ) {
        echo 'Author';
    } else if ( in_array ( $roles, 'subscriber' ) ) {
        echo 'Subscriber';
    }
}
3
  • Thanks. Now I see what the problem is. Your code works but it's showing the text based on the role the logged in user is, so if the user is a subscriber all my text shown for each comment says "Subscriber" or if admin is logged in, all the text says "Admin". Commented Mar 26, 2018 at 17:31
  • Ah ok, you want to show it based on the user being passed? I think it was a bit unclear in your current question @user2568633. To ensure I have this right, you're going to loop over a set of users and want to display their role accordingly per-user?
    – phatskat
    Commented Mar 27, 2018 at 13:34
  • phatskat, your last code might work but it breaks the site. I think there's something missing (semicolor or bracket?) but I've not been able to figure it out. Commented Apr 4, 2018 at 17:52
1

Checking against user roles is not recommended. Roles have capabilities, and it's the capability you need to check against.

E.g. if you want users who can publish posts to be able to do something, use current_user_can( 'publish_posts' ).

If an existing capability doesn't map on to what you're trying to do, you can add one, and this is how you should handle additional features.

Otherwise, you'll run into other issues, For example, a lot of users use manage_options as shorthand to detect if the user is an administrator, but on a multisite this capability changes, and may not do exactly what you expect.

If you must check for a user role however, you can do it this way, but it will only work after the init hook:

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    //The user has the "author" role
}

Remember, roles are not hierarchical, and are not user levels. Plugins can add additional roles, and what roles do can be changed by plugins

4
  • Thanks, your code works too but same problem, the text shown is based on what role the logged in user has rather than the comment author. I had not realized that was the issue I kept having until now. Commented Mar 26, 2018 at 17:34
  • ah so instead of fetching the current user, fetch the user with the ID of the author. Perhaps you should have asked "How do I make admin comments visually distinct from non-admin?" instead of asking about the attempted solution? There's a super easy solution to what you're trying to do that's completely unrelated to your question, but solves the original problem trivially, but that's not what you asked :(
    – Tom J Nowell
    Commented Mar 26, 2018 at 18:06
  • Sorry, I did not know that was the question I should have asked, I just realized the problem when the solutions for my questions were given. Commented Mar 27, 2018 at 12:47
  • see my other answer for a CSS based approach
    – Tom J Nowell
    Commented Mar 27, 2018 at 14:14
1

I'm working on a custom theme. For my comment section, if the user role is administrator I want to display "Admin", if subscriber "Subscriber", etc.

You can do it in pure CSS.

Look at the markup generated by WP for comments and you'll notice that it adds a class when the author adds a comment:

enter image description here

comment-author-admin is added to any comment authors who are also admins. This is added by the comment_class function in the default comments template, the same way the body_class and post_class functions work

Now we can use CSS and the :after selector to append the word 'Admin' to another element.

For example:

.comment.comment-author-admin cite:after {
    content: "Admin";
    margin-left: 0.5em;
    color: red;
    border: 1px solid red;
    border-radius: 6px;
    padding: 0 0.5em;
}

enter image description here

No PHP necessary.

As for current_user_can, the hint is in the function name current user can. AKA you, the current user of the site. It always refers to you, not the author of the content being worked on. Instead, you needed the comment author ID.

Thinking about it criticially, if it did work as you had expected, would it give you the current comment user? Or the current post user? What would happen if the comment had been made by a logged out user?

0

If you are trying to show the current user role, try this:

<?php
    $current_user = wp_get_current_user();
    global $wp_roles;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

?>

<?php echo $wp_roles->roles[ $user_role ]['name']; ?> //put this where you want your user role to show

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.