1

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 and a callback to format the comments output:

<?php if ( have_comments() ) : ?>
      <ol class="commentlist">
          <?php wp_list_comments( 'type=comment&callback=custom_comentarios' ); ?>
      </ol>
      <?php if ( ! comments_open() ) : ?>
          <p class="no-comments">Comments are closed</p>
      <?php endif; ?>
<?php endif; ?>

And this simple code on functions.php to format the comments output:

<?php
/* Custom callback function for displaying comments, see comments.php */
function custom_comentarios($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>
      <?php comment_text() ?>     
     </div>
<?php
}
?>

What do I need to place around this code to filter the comments and show them only to their owners? I have 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?

2 Answers 2

1

What you need to do to achieve this is to provide the second parameter of wp_list_comments(), which would be $comments and is described like this:

(array) (optional) Array obtained by get_comments query.
Default: The default return of get_comments.

Which actually makes it clear what you need to do, which would be utilizing get_comments(). Below a generic example on how to achieve this:

$user_id = get_current_user_id();
$user_specific_comments = get_comments(
    array(
        'user_id' => $user_id
    )
);
wp_list_comments(
    array(
        'per_page' => 3
    ),
    $user_specific_comments
);

The principle is clear now, so this should get you started.

2
  • Worked like a charm. I just had to add a couple of lines to format how I wanted and add my callback. Thanks.
    – lu-bhz
    Commented Oct 30, 2014 at 22:12
  • My pleasure. Yeah, the code is more proof of concept than actual implementation, but you got there, so it worked :) @lu-bhz Commented Oct 30, 2014 at 22:18
0

This allows private messaging between just the post author and the comment author... // https://wpquestions.com/Only_post_author_and_commentor_can_view_their_own_comments/10125 function restrict_comments( $comments , $post_id ){

global $post;

$user = wp_get_current_user();

if($post->post_author == $user->ID){

        return $comments;

}

foreach($comments as $comment){

    if(  $comment->user_id == $user->ID || $post->post_author == $comment->user_id  ){

        if($post->post_author == $comment->user_id){

            if($comment->comment_parent > 0){

                $parent_comm = get_comment( $comment->comment_parent );

                if( $parent_comm->user_id == $user->ID ){

                    $new_comments_array[] = $comment;       

                }

            }else{

                    $new_comments_array[] = $comment;   

            }

        }else{

            $new_comments_array[] = $comment;           

        }

    }

}

 return $new_comments_array; }



add_filter( 'comments_array' , 'restrict_comments' , 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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