Tutorials WordPress

Highlight Author Comments with Custom Styles

Updated

Written by

Dave Warfel

Reading Time

3 minutes

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

There are quite a few ways to customize WordPress comments, but one of the most common is to highlight author comments. When the post author responds in the comments section, you want it to stand out. This will add context, as well as help visitors find answers to previously asked questions.

WordPress’ comment_class Function

Out-of-the-box, most WordPress themes implement a standard function called comment_class(). This function adds several classes to each comment that is made on your site. Some example classes include:

  • comment
  • byuser
  • comment-author-{username}
  • thread-even or thread-odd
  • bypostauthor (if comment was made by the author of the post)
  • depth-# (with # being the depth level of the comment)
  • children (gets added to the containing element of all replies & threaded comments)

Highlight Author Comments

All we need to do is add some CSS to style our author comments. We’ll use the .bypostauthor class.

It depends how the CSS of your theme is setup. You might be able to just use .bypostauthor. Chances are, though, you’ll need to be more specific with your styles, and add at least one parent element. We’re using .comment-list as our parent item.

You can place this code in your theme’s style.css file, a custom CSS file or a plugin that provides custom CSS functionality (like Jetpack).

Adds a background color, a border & a slight inset shadow around all author comments:

.comment-list .comment.bypostauthor,
.comment-list .children .comment.bypostauthor {
	background: #f0f7fd;
	border: 1px solid #bce8f1;
	border-left: 5px solid #428bca;
	box-shadow: inset 0 0 0 1px #fff;
}

The 2nd selector (that has .children) is added because many themes add custom styles for threaded comments. This helps ensure that your author comment styling will override your theme’s default comment styling for threaded comments.

Highlight Author Comments on Multiple Author Blogs

Let’s say you manage a website alongside 2 other people. You each still write your own articles, but all 3 of you frequently comment on each other’s posts, and sometimes even answer your reader’s questions. You want ALL 3 of your comments to be highlighted, right? Not just the post author.

We can do this with the .comment-author-{username} class. Let’s say your 3 people’s usernames are sally, michelle & dave. Use this CSS (remember to check the styling for children/threaded comments):

.comment-list .comment.comment-author-sally,
.comment-list .comment.comment-author-michelle,
.comment-list .comment.comment-author-dave {
	background: #f0f7fd;
	border: 1px solid #bce8f1;
	border-left: 5px solid #428bca;
	box-shadow: inset 0 0 0 1px #fff;
}

Get Creative

There is no limit to what you can do here. Try using the pseudo-elements to really spice things up. Please post your examples in the comments so we can learn new tricks.

If you want to take things a step further, and add content in addition to styling, check out our post on adding content to author comments in WordPress.

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. 🏔️🏃🚴🤸

One response to “Highlight Author Comments with Custom Styles”

  1. Naim Avatar
    Naim

    Works like a charm! thanks for this tutorial!

Leave a Comment