Tutorials WordPress

Add Content to Post Author’s Comment

Updated

Written by

Dave Warfel

Reading Time

3 minutes

If you buy something from one of our links, we may earn a commission.

For sites with an active user base, customizing WordPress’ comment system can add great value. There are quite a few ways to customize author comments in WordPress. For this tutorial, I’ll show you how to add additional content to only the post author’s comments. This is one way to make the author’s comments standout on your WordPress blog.

Comments Template Files

First, we need to figure out which template files we’re going to edit. You’ll want to find your comment list output.

If you open up the comments.php file in your theme’s directory, it’s possible you’ll see your comment list in there. However, it’s much more likely you’ll just see a call to wp_list_comments(). If your theme already has created some custom HTML for your comments list, you’ll probably find it in the functions.php file.

Your call to wp_list_comments() will look like this:

wp_list_comments('callback=function_name');

Search in your functions.php file for whatever you see in place of “function_name”, and that’s where your custom comment output will be.

How to edit your comments list HTML output is beyond the scope of this article, but DigWP has you covered. We’ll assume you already have your custom output setup.

Add Something to the Comments Output

Check If Comment Belongs to the Author

Once you’ve got your custom comments output setup, the first thing we need to do is check to see if a comment belongs to the author of the post. At the very top of your file, let’s store two variables.

  • $comment_author_email – This stores the email address of the commenter
  • $post_author_email – This stores the email address of the post author

Inside your php tags, add the following:

$comment_author_email = get_comment_author_email();
$post_author_email = get_the_author_meta('user_email');

Now we setup a simple if statement to check if the comment author’s email is equal to the post author’s email.

<?php if ( $comment_author_email == $post_author_email ) { ?>
	// add your content here
<?php } ?> // end if is author

Add “Author” if Comment is by the Post Author

Here’s a better example of what our code looks like. We’re using the Bootstrap framework, so you’ll see some classes added for styling. Essentially, this code is outputting the comment author’s avatar, then their name (which links to their website, if they provided one), and IF it’s the post author, we’ve added a line break, followed by the word “Author.”

<div class="comment-meta pull-left">
	<?php echo get_avatar( $comment, 96 ); ?>
	<p class="text-center comment-author">
		<?php comment_author_link(); ?>
		<?php if ( $comment_author_email == $post_author_email ) { ?>
			<br /><small class="text-muted"><?php _e('Author', 'strapless'); ?></small>
		<?php } ?> // end if is author
	</p>
</div> <!-- .comment-meta -->
Custom WordPress Author Comments
Our custom author comments example adds the word “Author” below their name.

The _e( part is for translation. That’s beyond the scope of this article. If your site only needs to work in your native language, you can simply omit that, and just echo “Author”.

You can get creative & add any content you’d like. Just place it inside the if statement.

How have you customized your author’s comments? Please share your ideas below.

Dave Warfel

LinkedIn  •  X (Twitter)Dave has been working with WordPress since 2011. He's built 100s of client sites and almost a dozen of his own. He's tested almost every plugin you can think of, hosted with at least 10 different companies, and gone down every SEO rabbit hole you can imagine. When's he's not tinkering with new software, you'll find him in the mountains of Colorado, trail running, summiting peaks, and rippin' downhills on his mountain bike. 🏔️🏃🚴🤸

Leave a Comment