5 WordPress Comments Section Hacks

One thing that makes WordPress so dynamic is the ability for readers to easily post comments. This interaction between you and the readers is therefore an important element in your website and should be given the same attention as other sections of your website. So what can you do to improve the comments section on your WordPress website?

1. Removing HTML Links in User Comments

People, and especially bloggers, are always looking to promote themselves on other websites and no doubt will try to do that on yours as well. It can be a demanding and annoying task to have to go through each comment and remove links from them. By default, WordPress transforms URLs into clickable links, but you can add some piece of code to reverse this.

Navigate to the “function.php” file and add the code below:

function plc_comment_post( $incoming_comment ) {
	$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
	$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
	return( $incoming_comment );

function plc_comment_display( $comment_to_display ) {
	$comment_to_display = str_replace( ''', "'", $comment_to_display );
	return $comment_to_display;

add_filter('preprocess_comment', 'plc_comment_post', '', 1);
add_filter('comment_text', 'plc_comment_display', '', 1);
add_filter('comment_text_rss', 'plc_comment_display', '', 1);
add_filter('comment_excerpt', 'plc_comment_display', '', 1);

The above code filters out links by replacing HTML characters with HTML entities.

2. Use Twitter Avatars in Comments Section

Twitter has revolutionized how website owners communicate with visitors. In addition, visitors find it much easier to add their comments when Twitter functionality has been added. The first thing you will need to do is download the plug-in from this link.

After installing the plug-in and activating it, open the “comments.php” file and locate the comments loops. Paste the entire code from the file inside the section below:

<?php twittar('45', 'default.png', '#e9e9e9', 'twitavatars', 1, 'G'); ?>

You can also adjust the values inside “twittar();” such as the avatar size, the URL to use when an avatar cannot be retrieved as well as the border color.

You can also leverage the Disqus plugin to add more functionality and features to the comments system.

3. Style Author Comments

If your blog posts attract a lot of user comments, it can be a little difficult for readers to find author comments and responses if you do not have the threaded comments feature. One of the best ways to highlight author comments is by styling them differently. To do this, locate the “comments.php” file and locate the loop below:

<?php foreach comment as $comment) { ?>

Add the code below immediately after the comment loop above:

$isByAuthor = false;
if($comment->comment_author_email == get_the_author_email()) {
$isByAuthor = true;

Depending on your theme, find the line that represents a comment as shown below:

<li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">

Add the code below that checks if the email address used matches with the author’s address:

<li class="<?php echo $oddcomment; ?> <?php if($isByAuthor ) {
 echo 'authorcomment';} ?>" id="comment-<?php comment_ID() ?>">

If a match is found, the ‘authorcomment’ class is outputted.

Open the “style.css” file add your colors of choice to the author comments CSS class as shown below:


4. Display Number of Posts Publicly

To gain credibility or get attention from advertisers, you may want to publicly display the total number of comments on your posts and blog. Unfortunately, this information is only available to you internally. The solution? Locate where you would like the comments information to be displayed on your theme and paste the code below:

$count_posts = wp_count_posts();
$posts = $count_posts->publish;

$count_comments = get_comment_count();
$comments  = $count_comments['approved'];

echo "There's a total of ".$comments." comments on my blog, with an average ".round($comments/$posts)." comments per post.";

The above code retrieves the number of posts and comments from your blog and using some math, it is able to compute and display the average number of comments per post.

5. Display the Most Popular Posts from a Given Time Period

By default, WordPress allows you to display posts with the most number of comments as a side widget. Sometimes though, you may want to display popular posts from a certain time period, say in the month of February, 2012.

In order to do this, copy and paste the code below where you want the posts to be displayed on your theme:

$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2012-02-01' AND '2012-03-01' ORDER BY comment_count DESC LIMIT 0 , 10");

foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ($commentcount != 0) {
         <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>
    <?php }

Note the date settings on line 3 which you can customize to your own preferences.

Have you found any of these enhancements effective? Share your thoughts below:

Home CSS Deals HTML HTML5 Java JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress