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
2 Answers
Reset to default 1This 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
版权声明:本文标题:display user roles in comment form 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299405a1930447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论