8

I am relatively new to wordpress, and coding, and I am working on a site where the users will need to leave comments for certain posts, but the person I am building the site for wants the user to only be able to see the comments they have written for these specific posts, and not any comments from other users.

I've been searching around for a while now, and have not been able to find a solution that seems to work. Is there a way to get this to work that is simple enough for me to implement? Any help would be greatly appreciated.

Thanks!

2

Assuming your comment authors are intended to be registered users, the easiest way is probably to use a pre_get_comments action hook to modify the WP_Comment_Query object's user_id query var such that the query only returns comments from the current user:

function wpse262203_restrict_comment_views( $comments_query ) {
  // Don't interfere with comment results in the dashboard
  if( is_admin() )
    return;

  $current_user = wp_get_current_user(); // Get the current user

  if( $current_user instanceof WP_User && is_user_logged_in() ) {
    // The visitor is logged in as a WordPress user...

    // If they're an administrator, don't filter the comments
    if( in_array( 'administrator', $current_user->roles ) )
      return;

    // Otherwise, restrict queried comments to those authored by this user.
    $comments_query->query_vars[ 'user_id' ] = $current_user->ID;
  }
  else {
    // The visitor isn't logged in - make sure no comments are queried.
    $comments_query->query_vars[ 'comment__in' ] = array(0);
  }
}

add_action( 'pre_get_comments', 'wpse262203_restrict_comment_views' );

You can also use current_user_can() instead of/in addition to checking for user roles to tailor who comments are filtered for.

While you can also support anonymous commenting by using wp_get_current_commenter() in conjunction with the author_email WP_Comment_Query argument, this isn't terribly reliable or secure. Anonymous commenter ID data is stored in cookies, meaning the user can clear it or the cookies could expire - in which case the user would be unable to view their comments until they post another. The credentials are also fairly easily spoofed - crafty visitors could potentially gain access to comments from other users.


EDIT - Why this wasn't working, previously

After further investigation, my earlier attempts to use WP_Comment_Query::set() to change query variables was failing because as it turns out, WP_Comment_Query does not actually have a set() method, unlike it's WP_Query counterpart (see ticket #39120). It does however have a __call() "Magic Method", which was intercepting the call to the non-existent set() method and returning false, thus preventing the error which PHP would have normally thrown and confusing me to no end.

  • Hi Bosco, Thanks for your quick response. You were correct to assume that this would be for registered users. Sorry I didn't clarify in my original post. So I don't screw this up, where would this code be placed? In the comments.php file? – Keith Pocock Apr 3 '17 at 6:42
  • 1
    @KeithPocock This is an "action hook" - so it will execute when it needs to so long as WordPress processes the code. You can stick it in your theme's functions.php file, however if a 3rd-party created the theme, the changes could get overwritten when the theme updates. The best place to put this is in a new plugin - follow the "Getting Started" section of the Plugin Development Handbook (just the very top part of the page) to create a new plugin file, then paste the solution in my answer below the header information. – bosco Apr 3 '17 at 7:26
  • Hi Bosco, thanks for the clarification. I tried to place it into the functions.php file, but it didn't filter out the comments for a specific user when I tested it (most likely something I am doing wrong or not understanding haha). I will try it as a plugin this time, and will respond back with any results. Thanks again! – Keith Pocock Apr 3 '17 at 8:36
  • @KeithPocock Oops! I found a little bug in my code - it should work in either a plugin or functions.php now (though to be honest I haven't actually tested it O.o). Let me know how it goes! – bosco Apr 3 '17 at 22:22
  • 1
    Let us continue this discussion in chat. – bosco Apr 4 '17 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.