WordPress 后台仅允许管理员查看留言评论

默认WordPres除了管理员能浏览后台的评论,甚至连贡献者角色都可以看,确实有些不合理。如仅想让管理员有权限查看后台的评论留言,可将下面代码,添加到当前主题函数模板 functions.php中即可。

方法一

add_filter( 'comments_clauses', 'zm_restrict_comments', 10, 2 );
function zm_restrict_comments( $clauses, $wp_query ) {
	global $wpdb;
	if( ! current_user_can( 'manage_options' ) ) {
		$clauses['where'] .= " AND {$wpdb->prefix}comments.comment_type != 'comment'";
	}
	return $clauses;
}

添加上述代码后,非管理员,比如编辑和作者角色进入后台,打开评论菜单,里面将是空的。

方法二

// 重定向到仪表盘
function restrict_comments_access() {
	if ( ! current_user_can( 'manage_options' ) && strpos( $_SERVER['REQUEST_URI'], '/wp-admin/edit-comments.php') !== false ) {
		wp_redirect( admin_url() );
		exit;
	}
}
add_action( 'admin_init', 'restrict_comments_access' );

添加代码后,访问评论菜单链接,会重定向到仪表盘。

隐藏评论菜单

// 隐藏评论菜单
function restrict_comments_and_discussion_menus() {
	if ( ! current_user_can( 'manage_options' ) ) {
		remove_menu_page( 'edit-comments.php' );
	}
}
add_action( 'admin_menu', 'restrict_comments_and_discussion_menus' );

可以加上,把后台评论菜单也隐藏,但不加上面的代码,知道链接还是可以看到评论列表。

WordPress 后台仅允许管理员查看留言评论

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
  • WordPress
  • functions.php
    • 蓝莲花
      蓝莲花 3

      :wink: :wink: :wink: :wink: :wink:不错不错

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    拖动滑块以完成验证