WordPress贡献者审核评论

时间:2017-06-13 14:23:52

标签: wordpress comments user-permissions

如果用户拥有权限管理员页面中的“贡献者”可以查看等待审核的评论,但我需要禁用此功能。

在“查看评论等待审核”的用户角色中,它不存在任何内容。

如何为贡献者用户停用/wp-admin/edit-comments.php?comment_status=moderated?

enter image description here

4 个答案:

答案 0 :(得分:1)

您可以使用javascript为仅具有参与者角色的已登录用户添加脚本来绕过它,从而删除带有未经批准的dom类的评论行。

检查用户是否已登录且是贡献者:

if( is_user_logged_in() && current_user_can('contributor')) {

然后使用以下代码添加内联脚本或将js文件排入队列:

$('#the-comment-list tr.unapproved').remove();

检查注释是否在任何其他视图中可见,并将相应的类添加到脚本中以从任何位置删除它们

/ * edit * /

vanilla js script:

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]);
   }        
}

答案 1 :(得分:1)

修改评论查询

以下是一条建议,借助get_comments()操作调整pre_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'

取决于它是标签,评论查询变量还是它如何存储在数据库中,

修改评论计数

所有评论都在这里计算:

all

表示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;
}

对于无法发布帖子的用户,此操作还会从此处的管理菜单中删除计数编号:

pending

修改评论状态链接

最后,对于无法发布帖子的用户,我们可能希望删除待处理评论的状态链接:

add_filter( 'comment_status_links', function( $status )
{
    if( ! current_user_can( 'publish_posts' ) )
        unset( $status['moderated'] );
    return $status;
} );

所以它会变成:

all

希望它有所帮助!

答案 2 :(得分:0)

我找到了这个半解决方案,此代码隐藏了评论审核列表中的评论,但它删除了所有评论也审核了评论,现在还可以=)

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' );
}

答案 3 :(得分:0)

我建议您使用user role editor来完全控制用户角色,并为特定用户添加一些例外。

相关问题