WordPress 贡献者审核评论
WordPress Contributor Moderation Comments
如果用户在管理页面中有权限 "Contributor" 可以查看等待审核的评论,但我需要禁用它。
在具有 "View comments Awaiting Moderation" 的用户角色中,它什么都不存在。
如何为贡献者用户禁用 /wp-admin/edit-comments.php?comment_status=moderated?
您可以通过 javascript 仅针对具有贡献者角色的登录用户添加脚本来绕过它,从 dom.
中删除未经批准的 class 评论行
检查用户是否登录并且是贡献者:
if( is_user_logged_in() && current_user_can('contributor')) {
然后使用以下代码添加内联脚本或入队 js 文件:
$('#the-comment-list tr.unapproved').remove();
检查评论是否在任何其他视图中可见,并将相应的 classes 添加到脚本以从任何地方删除它们
/*编辑*/
香草 js 脚本:
var elem = document.getElementById("the-comment-list");
for (var i = 0; i < elem.childNodes.length; i++) {
if (/\bunapproved/.test(elem.childNodes[i].className)) {
elem.childNodes[i].parentNode.removeChild(elem.childNodes[i]);
}
}
我找到了这个半解决方案,这段代码从评论审核列表中隐藏了评论,但它删除了所有评论也审核了评论,现在没问题 =)
https://wordpress.stackexchange.com/questions/167250/prevent-contributor-to-show-comment-list
function filter_comments_by_contributor( $all_comments ) {
// get the current logged in user
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
return $all_comments;
} else {
// Logged in.
// check if the logged-in user is a contributor
if ( in_array( 'contributor', (array) $current_user->roles ) ) {
// check if the user is on wp-admin backend,
$screen = get_current_screen();
if ( ! empty( $screen ) && 'edit-comments' == $screen->id ) {
// get all posts by that contributor
$args = array(
'author' => $current_user->ID,
'posts_per_page' => - 1,
'fields' => 'ids'
);
$contributor_posts = get_posts( $args );
// unset the comments given on posts other than his/her.
foreach ( $all_comments as $key => $value ) {
if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
unset( $all_comments[ $key ] );
}
}
}
return $all_comments;
} else {
return $all_comments;
}
}
}
if( is_user_logged_in() && !current_user_can('manage_options')) {
add_filter( 'the_comments', 'filter_comment_by_contributor' );
}
我建议您使用 user role editor 来全面控制一般的用户角色,并为特定用户添加一些例外。
修改评论查询
这里有一个建议,在 pre_get_comments
操作的帮助下调整 get_comments()
输入参数的评论状态:
add_action( 'pre_get_comments', function( \WP_Comment_Query $query )
{
// Only target edit-comments.php
if( ! did_action( 'load-edit-comments.php' ) )
return;
// Only target users that can't publish posts
if( current_user_can( 'publish_posts' ) )
return;
// Halt the query for pending comments
if( 'hold' === $query->query_vars['status'] )
$query->query_vars['status'] = 'non-existent';
// Remove pending comments from the 'all' or '' status view
if( in_array( $query->query_vars['status'], [ 'all', '' ], true ) )
$query->query_vars['status'] = 'approve';
} );
我们只针对 edit-comments.php
页面并将状态修改为 approve
(如果它为空)或 'all'
(对于无法发布帖子的用户)。
这里我们将评论状态分配给一个不存在的状态,以删除待处理评论列表。
待定评论的各种状态值可能有点混乱,例如
hold, pending, moderated, '0'
取决于它是标签、评论查询变量还是它在数据库中的存储方式,
修改评论数
所有 条评论计数:
表示Approved + Pending
条评论的总和。
当我们像上面那样更改评论查询时,这些评论状态计数不会改变。我们可能也想调整它。
这是我们如何通过 wp_count_comments
过滤器调整评论数的示例:
add_filter( 'wp_count_comments', 'wpse_count_comments', 10, 2 );
function wpse_count_comments( $counts, $post_id )
{
// Only target the backend
if( ! is_admin() )
return $counts;
// Only target users that can't publish posts
if( current_user_can( 'publish_posts' ) )
return $counts;
// Avoid infinite loop before calling wp_count_comments()
remove_filter( current_filter(), __FUNCTION__ );
$counts = wp_count_comments( $counts, $post_id );
add_filter( current_filter(), __FUNCTION__, 10, 2 );
// Subract 'moderated' count from 'all' count
$counts->all = $counts->all - $counts->moderated;
// Set 'moderated' count to zero
$counts->moderated = 0;
return $counts;
}
对于无法发布帖子的用户,这还将从此处的管理菜单中删除计数:
修改评论状态links
最后,对于无法发布帖子的用户,我们可能希望删除 待处理 评论的 link 状态:
add_filter( 'comment_status_links', function( $status )
{
if( ! current_user_can( 'publish_posts' ) )
unset( $status['moderated'] );
return $status;
} );
所以会变成:
希望对您有所帮助!
如果用户在管理页面中有权限 "Contributor" 可以查看等待审核的评论,但我需要禁用它。
在具有 "View comments Awaiting Moderation" 的用户角色中,它什么都不存在。
如何为贡献者用户禁用 /wp-admin/edit-comments.php?comment_status=moderated?
您可以通过 javascript 仅针对具有贡献者角色的登录用户添加脚本来绕过它,从 dom.
中删除未经批准的 class 评论行检查用户是否登录并且是贡献者:
if( is_user_logged_in() && current_user_can('contributor')) {
然后使用以下代码添加内联脚本或入队 js 文件:
$('#the-comment-list tr.unapproved').remove();
检查评论是否在任何其他视图中可见,并将相应的 classes 添加到脚本以从任何地方删除它们
/*编辑*/
香草 js 脚本:
var elem = document.getElementById("the-comment-list");
for (var i = 0; i < elem.childNodes.length; i++) {
if (/\bunapproved/.test(elem.childNodes[i].className)) {
elem.childNodes[i].parentNode.removeChild(elem.childNodes[i]);
}
}
我找到了这个半解决方案,这段代码从评论审核列表中隐藏了评论,但它删除了所有评论也审核了评论,现在没问题 =)
https://wordpress.stackexchange.com/questions/167250/prevent-contributor-to-show-comment-list
function filter_comments_by_contributor( $all_comments ) {
// get the current logged in user
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
return $all_comments;
} else {
// Logged in.
// check if the logged-in user is a contributor
if ( in_array( 'contributor', (array) $current_user->roles ) ) {
// check if the user is on wp-admin backend,
$screen = get_current_screen();
if ( ! empty( $screen ) && 'edit-comments' == $screen->id ) {
// get all posts by that contributor
$args = array(
'author' => $current_user->ID,
'posts_per_page' => - 1,
'fields' => 'ids'
);
$contributor_posts = get_posts( $args );
// unset the comments given on posts other than his/her.
foreach ( $all_comments as $key => $value ) {
if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
unset( $all_comments[ $key ] );
}
}
}
return $all_comments;
} else {
return $all_comments;
}
}
}
if( is_user_logged_in() && !current_user_can('manage_options')) {
add_filter( 'the_comments', 'filter_comment_by_contributor' );
}
我建议您使用 user role editor 来全面控制一般的用户角色,并为特定用户添加一些例外。
修改评论查询
这里有一个建议,在 pre_get_comments
操作的帮助下调整 get_comments()
输入参数的评论状态:
add_action( 'pre_get_comments', function( \WP_Comment_Query $query )
{
// Only target edit-comments.php
if( ! did_action( 'load-edit-comments.php' ) )
return;
// Only target users that can't publish posts
if( current_user_can( 'publish_posts' ) )
return;
// Halt the query for pending comments
if( 'hold' === $query->query_vars['status'] )
$query->query_vars['status'] = 'non-existent';
// Remove pending comments from the 'all' or '' status view
if( in_array( $query->query_vars['status'], [ 'all', '' ], true ) )
$query->query_vars['status'] = 'approve';
} );
我们只针对 edit-comments.php
页面并将状态修改为 approve
(如果它为空)或 'all'
(对于无法发布帖子的用户)。
这里我们将评论状态分配给一个不存在的状态,以删除待处理评论列表。
待定评论的各种状态值可能有点混乱,例如
hold, pending, moderated, '0'
取决于它是标签、评论查询变量还是它在数据库中的存储方式,
修改评论数
所有 条评论计数:
表示Approved + Pending
条评论的总和。
当我们像上面那样更改评论查询时,这些评论状态计数不会改变。我们可能也想调整它。
这是我们如何通过 wp_count_comments
过滤器调整评论数的示例:
add_filter( 'wp_count_comments', 'wpse_count_comments', 10, 2 );
function wpse_count_comments( $counts, $post_id )
{
// Only target the backend
if( ! is_admin() )
return $counts;
// Only target users that can't publish posts
if( current_user_can( 'publish_posts' ) )
return $counts;
// Avoid infinite loop before calling wp_count_comments()
remove_filter( current_filter(), __FUNCTION__ );
$counts = wp_count_comments( $counts, $post_id );
add_filter( current_filter(), __FUNCTION__, 10, 2 );
// Subract 'moderated' count from 'all' count
$counts->all = $counts->all - $counts->moderated;
// Set 'moderated' count to zero
$counts->moderated = 0;
return $counts;
}
对于无法发布帖子的用户,这还将从此处的管理菜单中删除计数:
修改评论状态links
最后,对于无法发布帖子的用户,我们可能希望删除 待处理 评论的 link 状态:
add_filter( 'comment_status_links', function( $status )
{
if( ! current_user_can( 'publish_posts' ) )
unset( $status['moderated'] );
return $status;
} );
所以会变成:
希望对您有所帮助!