允许用户仅管理自己帖子的评论

发布于 2024-12-11 13:53:35 字数 744 浏览 0 评论 0原文

我想限制贡献者和作者用户只能查看和管理自己帖子的评论。

我尝试过以下过滤器但没有成功。

//Manage Your Own Comments Only
    function myplugin_comments_thisuseronly( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit-comments.php' ) !== false ) {
    if ( !current_user_can( 'edit_others_posts' ) ) {
        global $current_user;global $wpdb;
        $usersposts = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM wp_posts WHERE post_author = %s", $current_user->id) );
        $wp_query->set('comment_post_ID', array($usersposts));
    }
}
}

add_filter('parse_query', 'myplugin_comments_thisuseronly');

我也尝试过

$wp_query->set('comment_post_ID__in', array($usersposts));

I want to restrict contributer and author users to seeing and manageing comments only on their own posts.

I have tried the following filter with no success.

//Manage Your Own Comments Only
    function myplugin_comments_thisuseronly( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit-comments.php' ) !== false ) {
    if ( !current_user_can( 'edit_others_posts' ) ) {
        global $current_user;global $wpdb;
        $usersposts = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM wp_posts WHERE post_author = %s", $current_user->id) );
        $wp_query->set('comment_post_ID', array($usersposts));
    }
}
}

add_filter('parse_query', 'myplugin_comments_thisuseronly' );

I also tried

$wp_query->set('comment_post_ID__in', array($usersposts));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-12-18 13:53:35

好吧,我明白了。

首先,用户需要拥有有权编辑已发布帖子和审核评论的角色。后者对于作者/贡献者来说不是标准。

然后,通过过滤获取评论的查询(使用 comments_clauses)并添加联接,您可以仅显示当前登录用户的帖子的评论。

function my_plugin_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;
};
// Ensure that editors and admins can moderate all comments
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}

工作完美。角色的更改可能并不适合所有设置,但碰巧它确实适合我在这个网站上。

Ok, I figured this out.

First, the user needs to have a role with permission to edit published posts AND to moderate comments. The latter is not standard for authors/contributors.

Then, by filtering the query which gets the comments (using comments_clauses) and adding a join you can show only comments for posts by the currently logged in user.

function my_plugin_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;
};
// Ensure that editors and admins can moderate all comments
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}

Works perfectly. The changes to roles may not suit every setup, but as it happens it does work for me on this site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文