×

Code nhận biết Admin khi bình luận trên WordPress

Code nhận biết Admin khi bình luận trên Wordpress caodem.com

Chào các bạn, hôm nay mình sẽ giới thiệu cho các bạn một đoạn code ngắn nhưng khá hay, đó chính là code thêm chức năng nhận biết bình luận của khách hoặc quản trị viên trên trình bình luận mặc định của WordPress.

Code nhận biết Admin khi bình luận

Đầu tiên các bạn sao chép đoạn code bên dưới, dán vào file function.php trong thư mục theme mà bạn đang sử dụng.

// phan biet admin khi binh luan
if ( ! class_exists( 'WPB_Comment_Author_Role_Label' ) ) :
class WPB_Comment_Author_Role_Label {
public function __construct() {
add_filter( 'get_comment_author', array( $this, 'wpb_get_comment_author_role' ), 10, 3 );
add_filter( 'get_comment_author_link', array( $this, 'wpb_comment_author_role' ) );
}

function wpb_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 = ' <span class="comment-author-label comment-author-label-'.$comment_user_role.'">' . ucfirst($comment_user_role) . '</span>';
} else { 
$this->comment_user_role = 'Guest';
} 
return $author;
} 
function wpb_comment_author_role($author) { 
return $author .= $this->comment_user_role; 
} 
}
new WPB_Comment_Author_Role_Label;
endif;
// ket thuc

Sau khi thêm đoạn code trên bạn thử mở bình luận trên bài viết của bạn ra, xem đã hiển thị thêm dòng nhận biết chưa nhé.

Thay đổi màu chữ nhận biết từng nhóm người khi bình luận

Tiếp theo bạn sao chép đoạn css bên dưới rồi dán vào file style.css trong thư mục theme mà bạn đang sử dụng.

/* thay doi mau chu nhan biet khi binh luan cho tung nhom nguoi */
.comment-author-label-administrator {color:#ff3333;} /* mau chu admin */
.comment-author-label-editor { color:#666666;} /* bien tap vien */
.comment-author-label-author {color:#666666;} /* tac gia */
.comment-author-label-contributor {color:#444444; } /* cong tac vien */
.comment-author-label-subscriber {color:#00cc00;} /* thanh vien dang ky */

Sau khi hoàn thành thì nhớ kiểm tra xem code đã hoạt động chưa nhé, nếu code nhận biết Admin gây lỗi giao diện trên trang web của bạn, thì bạn chỉ cần xóa code đi là xong.
Chúc bạn thành công!

2452
"Nếu bạn cảm thấy bài viết này hữu ích, bạn có thể ủng hộ cho tác giả một tách Cafe"
THANH TOÁN ỦNG HỘ
Sacombank
NGUYEN NGOC HOAN
040086597979
Đọc thêm bài khác
×
Chia sẻ trang này tới
QR:  Code nhận biết Admin khi bình luận trên WordPress

2 Bình luận
  1. M.Studio CÁO

    uan if ( ! class_exists( ‘WPB_Comment_Author_Role_Label’ ) ) : class WPB_Comment_Author_Role_Label { public function __construct() { add_filter( ‘get_comment_author’, array( $this, ‘wpb_get_comment_author_role’ ), 10, 3 ); add_filter( ‘get_comment_author_link’, array( $this, ‘wpb_comment_author_role’ ) ); } function wpb_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 = ‘Guest’; } return $author; } function wpb_comment_author_role($author) { return $author .= $this->comment_user_role; } } new WPB_Comment_Author_Role_Lab

  2. M.Studio CÁO

    lỗi code rồi ad



Gọi điệnMessengerZalo