How to show user [Commentator] role in commment page of wordpress

i am trying to show wordpress registered users role like – subscriber, author, editor etc.

into comment page of wordpress.

Read More

for that i created this code

<?php //get the commented user id
$user_id   = get_comment(get_comment_ID())->user_id;

if ($user_id)
{
    $user_info = get_userdata($user_id );
    echo implode(', ', $user_info->roles) ;
} ?>

it is working fine for me

If commentator’s name is KING KHAN and he is subscriber then it shows

KING KHAN [subscriber]

but if the user is being deleted then into the comment page in comment of that user it shows

KING KHAN[ Warning: implode(): Invalid arguments passed in /home/USERACCOUNT/public_html/wp-includes/class-walker-comment.php on line 277]

Here instead of this i want to show

KING KHAN[Member Deleted]

please edit above code for this

Related posts

2 comments

  1. <?php //get the commented user id
    $user_id   = get_comment(get_comment_ID())->user_id;
    
    if ($user_id)
    {
        $user_info = get_userdata($user_id );
        if ( isset($user_info->roles) ) {
           echo implode(', ', $user_info->roles) ;
        } else {
           echo 'Member Deleted';
        }
    } ?>
    
  2. You need to use condition as I did on following code.

    if ($user_id)
    {
        $user_info = get_userdata($user_id );
        if(isset($user_info) && is_array($user_info->roles)) {
           echo implode(', ', $user_info->roles) ;
        }else{
           echo "Member Deleted";
        }
    
    }
    

Comments are closed.