0

in my comments I wanna show different avatars depending on the user role that the person has, that was giving upto the comment (not the role of the logged in user).

I tried like it follows inside comments loop:

   <?php
   $commentator_id = get_comment(get_comment_ID())->user_id;
   $commentator_info = get_userdata($commentator_id);
   $capabilities = $user_info->wp_capabilities;
   if (array_key_exists('editor', $capabilities)) {
      // echo markup here
   }
?>

But it doesn't function. Does anybody know why ?

0

What's the variable $user_info in you code? Probably you mean $commentator_info? If yes, this is certainly the error. You are trying to extract wp_capabilities (?) from a non-initialized variable ($user_info).

Try this:

    <?php
   $comment_id = get_comment_ID();
   $comment_data = get_comment($comment_id);
   $commentator_id = $comment_data->user_id;
   $commentator_info = get_userdata($commentator_id); // get_userdata, with the user ID specified, returns a WP_User object.

   if ( user_can($commentator_info, 'editor') ) {  // user_can accept the user ID or the whole user object ($commentator_info).
      // echo markup here
   }

Changes: added $comment_id and $comment_data variables (now it's more readable), deleted $capabilities (you don't need to store the capabilities in a variable, you can check them directly passing the whole user object (or the user ID) and the capability you want to verify to user_can(). Added user_can() and removed the array-key check.

  • Sorry it only checks the logged in user and n ot the user, that was posting the comment...What to do ? – RodneyHawk Oct 31 '16 at 10:12
  • Sorry, so you want to operate on user not-logged in? A not-logged user cannot have a capability such as "editor"; they are all visitors/guests. What's the point? – BlackOut Oct 31 '16 at 10:16
  • No, I want, that for a user that is a subscriber and was writing a comment, another image near his comment is shown as for an user that was writing a comment and is an editor. The query has to be whitch role has the user, of this the comment ist ! – RodneyHawk Oct 31 '16 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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