1

Is there any way to display different data in an author.php profile template depending on the role of the profile displayed? (NOT of the person viewing it, but the author that particular profile refers to).

If a profile is being displayed of an author with role editor, then some content would be displayed, and if the profile belongs to an author with role contributor, then some other stuff would be displayed.

I haven't found an answer for this anywhere yet. Can it be done?

2 Answers 2

1

current_user_can does not give the desired output when working with roles. There is a trac ticket #22624 explaining this all. It was closed with the following

  • Keywords close removed

  • Milestone Awaiting Review deleted

  • Resolution set to wontfix

  • Status changed from new to closed

My solution would be to get the role of the featured user and then check that against the specific roles

The first thing would be to retrieve the author's role with get_queried_object()->roles and then check of the desired role is in array, and if it is, do something. Here is the code

$user_role = get_queried_object()->roles[0];

if( in_array( strtolower('Editor'), $user_role ) ) {
    //Do something for Editor
}elseif( in_array( strtolower('Contributor'), $user_role ) ) { 
    //Do something for Contributor
}

EDIT

User roles starts with uppercase letters, but the roles returned from get_queried_object()->roles starts with lowercase letters. That is why I've use the roles as such in my code and used strtolower to convert the roles to lowercase

3
  • This is the perfect solution. Works wonderfully. Commented Oct 10, 2014 at 8:54
  • I can't seem to get this to work... <?php $user_role = get_queried_object()->roles; ?> <?php if( in_array( strtolower('Teacher'), $user_role ) ) { ?> teacher <?php } elseif( in_array( strtolower('School'), $user_role ) ) { ?> school <?php } ?>
    – Pete
    Commented Apr 18, 2016 at 15:05
  • @Pete There is a slight error, I have fixed it in my code, get_queried_object()->roles returns an array, so you should access the first value with get_queried_object()->roles[0] Commented Apr 18, 2016 at 16:54
0

Can it be done?

Sure.

Inside author.php get the user id who viewed profile refers to, using get_queried_object_id() check the capability and finally conditionally display content based on that.

I said "check the capability" and not "check the role" because check roles is a meh thing, it's a lot better and recommended check for capabilities. Use user_can for the scope.

2
  • Unless you have 3 roles with exactly the same capabilities, but you need them to be named differently and query them separately, in which case, your answer is of no use, and Pieter's works perfectly. Commented Oct 10, 2014 at 13:31
  • I stand corrected. Apologies, and thanks. Duly noted. Commented Oct 10, 2014 at 14:00

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.