2

My question is exactly the same as this, not sure if it can be bumped so thought I'd post again.

Basically I'm trying to create a scenario similar to how comments work. On a page, a registered user can submit a comment, except, only they can see it, no one else. These comments would be called notes.

Is it possible to change the comments so that only the submitted user can see it? I am mainly looking for something that's right under a post, page, that I can put in a template.

  • While I think that is a very neat idea, technically plugin recommendations are off-topic. – helgatheviking Nov 6 '13 at 0:04
  • I have removed the plugin recommendation part, because that would be off topic. – fuxia Nov 6 '13 at 1:40
0

I used _s as a template. The comments are loaded with wp_list_comments( array( 'callback' => '_s_comment' ) ); and then comments are styled with _s_comment(). That is what you will use.

You can use get_current_user_id() to get the current user id.

$current_user_id = get_current_user_id();

You can get the user id of the commenter with $comment->user_id. You then check that they both match.

if ( $current_user_id == $comment->user_id )

The two changes that I made are in line 11 and 20.

I have added the code in a gist and below.

<?php
if ( ! function_exists( '_s_comment' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 */
function _s_comment( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;

    $current_user_id = get_current_user_id();

    if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>

    <li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
            <div class="comment-body">
                    <?php _e( 'Pingback:', '_s' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', '_s' ), '<span class="edit-link">', '</span>' ); ?>
            </div>

    <?php elseif ( $current_user_id == $comment->user_id ) : ?>

    <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
            <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
                    <footer class="comment-meta">
                            <div class="comment-author vcard">
                                    <?php if ( 0 != $args['avatar_size'] ) { echo get_avatar( $comment, $args['avatar_size'] ); } ?>
                                    <?php printf( __( '%s <span class="says">says:</span>', '_s' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
                            </div><!-- .comment-author -->

                            <div class="comment-metadata">
                                    <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
                                            <time datetime="<?php comment_time( 'c' ); ?>">
                                                    <?php printf( _x( '%1$s at %2$s', '1: date, 2: time', '_s' ), get_comment_date(), get_comment_time() ); ?>
                                            </time>
                                    </a>
                                    <?php edit_comment_link( __( 'Edit', '_s' ), '<span class="edit-link">', '</span>' ); ?>
                            </div><!-- .comment-metadata -->

                            <?php if ( '0' == $comment->comment_approved ) : ?>
                            <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', '_s' ); ?></p>
                            <?php endif; ?>
                    </footer><!-- .comment-meta -->

                    <div class="comment-content">
                            <?php comment_text(); ?>
                    </div><!-- .comment-content -->

                    <?php
                            comment_reply_link( array_merge( $args, array(
                                    'add_below' => 'div-comment',
                                    'depth'     => $depth,
                                    'max_depth' => $args['max_depth'],
                                    'before'    => '<div class="reply">',
                                    'after'     => '</div>',
                            ) ) );
                    ?>
            </article><!-- .comment-body -->

    <?php
    endif;
}
endif; // ends check for _s_comment()
  • Thanks a lot!! Just saw this haha made my day thanks again – Jake Dec 20 '13 at 3:19

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.