Creating a private comment form

I was working on a school website with student’s being given activities and I needed to facilitate communication with their teachers that was private.

First I tried to make a new type of comments with ACF, advanced custom fields, that proved troublesome because of needing to fetch them to specific users.

Then I messed around with only showing the users their own comments and stumbled across Ashfame’s solution and it is great and I thank him.

To display the comments in posts or pages there is a shortcode
[show_recent_comments count=15]

I had to tweak the code so that it showed only the current post’s comments and I didn’t need a permalink structure so this is what I added to functions.php

add_shortcode ( 'show_recent_comments', 'show_recent_comments_handler' );

function show_recent_comments_handler( $atts, $content = null )
{
extract( shortcode_atts( array( 
"count" => 10,
		
		
	      "pretty_permalink" => 0
		), $atts ));

	$output = ''; // this holds the output
	
	if ( is_user_logged_in() )
	{
		global $current_user;
		get_currentuserinfo();

		$args = array(
			'user_id' => $current_user->ID,
			'post_id' => $post_id = $GLOBALS['post']->ID, // ID is what post wanted
			'number' => $count, // how many comments to retrieve
			'status' => 'approve'
			);

		$comments = get_comments( $args );
		if ( $comments )
		{
			$output.= "<ul>\n";
			foreach ( $comments as $c )
			{
			$output.= '<li>';			
			$output.= $c->comment_content;
			$output.= "</li>\n";
			}
			$output.= '</ul>';
		}
	}
	else
	{
		$output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
		$output.= '<h2><a href="'.get_settings('siteurl').'/wp-login.php?redirect_to='.get_permalink().'">Login Now &rarr;</a></h2>';
	}
	return $output;
}

Related Posts