0

I'm building a site for a research and my participants will be all logged in to use the site. I would like to make these participants able to see only their own comments, so other users' comments will be hidden.

I'm using the comments.php file with, among other things, this simple code:

<?php if ( have_comments() ) : ?>

      <ol class="commentlist">
          <?php wp_list_comments(); ?>
      </ol>

      <?php if ( ! comments_open() ) : ?>
          <p class="no-comments">Comments are closed</p>
      <?php endif; ?>

<?php endif; ?>

What do I need to place around this code to filter the comments and show them only to their owners? Thanks.

1 Answer 1

1

You need to edit the comments template and use a combination of this

get_comment(get_comment_ID())->user_id;

which returns the user id of whoever commented, and compare it to the current user with

get_current_user_id();

Both return 0 if the commenter wasn't registered or if the current user isn't logged in, respectively.

3
  • Thanks, but I forgot to mention that I'm not a programmer and I don't know php syntax :-/ I know that it will be something like: get the current user, check if there's a comment from the current user on the current post, if true then show comment. But how to write this in php?
    – lu-bhz
    Commented Oct 29, 2014 at 14:20
  • Sorry, I can't write the code for you, but maybe someone else will.
    – vcanales
    Commented Oct 29, 2014 at 15:54
  • Thanks, I'll ask it elsewhere.
    – lu-bhz
    Commented Oct 30, 2014 at 17:32

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