admin管理员组

文章数量:1122832

I am running into a problem in that I have users that can edit comments, but only those placed onto their own posts. Which is what I want. Unfortunately the edit-comments.php page shows all comments. Is there a way to hide/remove the comments that the user can't manage anyway?

I tried:

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_commentsment_post_ID = wp_posts.ID";
}
return $clauses;
}

if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}

But this has no effect

I am running into a problem in that I have users that can edit comments, but only those placed onto their own posts. Which is what I want. Unfortunately the edit-comments.php page shows all comments. Is there a way to hide/remove the comments that the user can't manage anyway?

I tried:

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

if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}

But this has no effect

Share Improve this question edited Apr 10, 2024 at 12:03 Klein asked Apr 10, 2024 at 8:08 KleinKlein 114 bronze badges 3
  • note that the post author can already be set via post_author__in, you don't need to mess with the raw SQL to do that when we already have pre_get_comments and WP_Comment_Query. It would also be much more afficient to move the current_user_can check into the function so that it isn't broken by being super early or modifications made to the user state. It would also be better to fetch the current user ID using the proper function rather than bypassing it and using a global variable developer.wordpress.org/reference/functions/get_current_user_id – Tom J Nowell Commented Apr 10, 2024 at 8:46
  • I tried moving the check into the function, and that too doesn't work. I got this function from a website claiming this was the answer, but it doesn't work. I don't need to know how to set the person as the author, I want the comments.php in the dashboard to not show comments from posts the user isn't the author of. – Klein Commented Apr 10, 2024 at 10:15
  • I did not suggest setting/changing the author of the post, what I suggested was what the code you found is trying to do with raw SQL but instead using the proper APIs/filters. I've written an answer that might make more sense – Tom J Nowell Commented Apr 10, 2024 at 10:37
Add a comment  | 

2 Answers 2

Reset to default 0

I found another piece of code on the internet which I have modified to work.

add_filter('the_comments', 'edit_comments_filter_comments');

function edit_comments_filter_comments($comments){
    global $pagenow;

    $currentuserid = get_current_user_id();

    if($pagenow == 'edit-comments.php' && !current_user_can('edit_others_posts')){
        foreach($comments as $i => $comment){
            $the_post = get_post($comment->comment_post_ID);
            if($comment->user_id != $currentuserid  && $the_post->post_author != $currentuserid)
                unset($comments[$i]);
        }
    }
    return $comments;
}

As I understand it, this takes the comments pulled in by the query and checks them before displaying them.

Instead of modifying raw SQL it's easier to add a parameter to the comment query combined with a condition in pre_get_comments:

https://developer.wordpress.org/reference/hooks/pre_get_comments/

E.g. this filter modifies the comment query in the admin area to only show comments on posts that belong to you:

add_action( 'pre_get_comments', 'only_your_posts_comments_in_admin' );
function only_your_posts_comments_in_admin( WP_Comment_Query $query ) : void {
    if ( ! is_admin() ) {
        return; // we only want to change admin screens
    }

    // limit to just the comments I made:
    $query->set( 'user_id', get_current_user_id() );
}

You can then modify that function to check if the user can/can't modify other peoples posts via current_user_can.

本文标签: dashboardHide comments from admin commentsphp that user can39t edit or manage