admin管理员组

文章数量:1122832

how can i show user's role in comment form near their name?!! for example when admin reply a comment i want to show "admin" word near admin name or when a user buy a product from my site and send a comment i want to show "buyer" role in near name

how can i show user's role in comment form near their name?!! for example when admin reply a comment i want to show "admin" word near admin name or when a user buy a product from my site and send a comment i want to show "buyer" role in near name

Share Improve this question asked Sep 29, 2016 at 7:35 m.javad Koushkim.javad Koushki 731 silver badge9 bronze badges 4
  • 2 It seems you haven't try to search this by yourself! Anyway read this post stackoverflow.com/questions/34037576/… – Rishabh Commented Sep 29, 2016 at 7:40
  • i did but it doesnt work yet please tell me what do i exactlly to do?! – m.javad Koushki Commented Sep 29, 2016 at 10:02
  • What exactly have you already done? Without seeing what you've attempted for yourself it is hard to offer help. You might find How to Ask useful. – Andy Macaulay-Brook Commented Sep 29, 2016 at 10:30
  • i dont anything because i dont know.just know i need add something to comments.php file to display user role – m.javad Koushki Commented Sep 29, 2016 at 18:49
Add a comment  | 

2 Answers 2

Reset to default 1

This will print User Role

global $current_user;
$user_role = $current_user->roles[0];
echo $user_role;

Use above code, where ever you want to display user role.

With this code, I have the user role next to the name in the comments section, but instead of displaying the administrator’s name, I want it to display the desired name that I want. What should I do?

/** Add User Role to Comments. */
if ( ! class_exists( 'Comment_Author_Role_Label' ) ) :

    class Comment_Author_Role_Label {
    
        public function __construct() {
            add_filter( 'get_comment_author', array( $this, 'get_comment_author_role' ), 10, 3 );
            add_filter( 'get_comment_author_link', array( $this, 'comment_author_role' ) );
        }
        
        function get_comment_author_role($author, $comment_id, $comment) {
            $authoremail = get_comment_author_email( $comment);
            if (email_exists($authoremail)) {
                $commet_user_role = get_user_by( 'email', $authoremail );
                $comment_user_role = $commet_user_role->roles[0];
                $this->comment_user_role = ' ' . ucfirst($comment_user_role) . '';
            } else {
                $this->comment_user_role = '';
            }
            return $author;
        }
        
        function comment_author_role($author) {
            return $author .= $this->comment_user_role;
        }
    }
    new Comment_Author_Role_Label;
    
endif;

本文标签: display user roles in comment form