任何问题请联系WX:uu16853

买模板送网站采集器 让你轻松放开双手运营网站!

购买更多有优惠!

让作者在 WordPress 后台只能看到自己文章下的评论

对于多作者博客,默认情况下,在WordPress后台的评论列表是可以看到其他作者文章下的评论的,如果要限制用户只能看到自己文章下的评论,将下面的代码添加到主题根目录下的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 让作者在 WordPress 后台只能看到自己文章下的评论
 * https://www.wpdaxue.com/show-comments-authors-own-posts.html
 */
function wpdx_get_comment_list_by_user($clauses) {
	if (is_admin()) {
		global $user_ID, $wpdb;
		$clauses['join'] = ", wp_posts";
		$clauses['where'] .= " AND wp_posts.post_author = ".$user_ID." AND wp_comments.comment_post_ID = wp_posts.ID";
	};
	return $clauses;
};
if(!current_user_can('edit_others_posts')) {
	add_filter('comments_clauses', 'wpdx_get_comment_list_by_user');
}

/**
* 让作者在 WordPress 后台只能看到自己文章下的评论
* https://www.wpdaxue.com/show-comments-authors-own-posts.html
*/
function wpdx_get_comment_list_by_user($clauses) {
if (is_admin()) {
global $user_ID, $wpdb;
$clauses[‘join’] = ", wp_posts";
$clauses[‘where’] .= " AND wp_posts.post_author = ".$user_ID." AND wp_comments.comment_post_ID = wp_posts.ID";
};
return $clauses;
};
if(!current_user_can(‘edit_others_posts’)) {
add_filter(‘comments_clauses’, ‘wpdx_get_comment_list_by_user’);
}

注:你可能需要修改 第 8-9 行中的几个 wp_ 为你自己的数据库前缀。第 13 行添加了判断,只有“编辑”角色以下的用户才生效,也就是说,属于编辑和管理员这两个角色的用户是不限制的。

100% Secure Checkout

PayPal / MasterCard / Visa