Paulund
2012-07-09 #wordpress

Customizing WordPress Comments

Post comments are an important part of any blog, they give your readers a way of engaging with the author of the article. Comments are the main thing that makes a blog different to a normal website, websites will display information about a subject but don't normally allow readers to engage with the author. This doesn't give your readers a way to discuss any points the author makes in the article, this is where comments come in and are important for the growth of your blog.

Why Are They Important?

Comments allow your readers to provide their expertise to add additional points to the author article that they might of forgot. A good example of this is on this blog, here I blog about code snippets and web development tutorials. This type of blog it is so important to have comments as it means the readers can hopefully show me a better way to code what I'm trying to do. Blogs are a two way conversion with the author and the reader I learn so much from the readers of Paulund and I hope they learn as much from my blog posts as I do.

Problems With Wordpress Comments

Above I have spoke about the benefits of having blog comments turned on and this is a massive benefit which is why the majority of blogs have comments. But with the benefit of comments there is also a lot of disadvantages of comments especially with Wordpress. ### Automated Comment Spam

This is main problem with Wordpress comments and because Wordpress is so popular it makes it an easy target for automated software crawling the web look for Wordpress and comment on the posts. By default Wordpress allows any commenter to leave a comment with the following fields: Name, email, website and comment. When these are displayed the website url will be made into a link around the commenter name making this an easy way to get links back to your website. If you have a Wordpress blog you will see that as soon as you start getting visitors to your blog you will see the comment spam. These are fake comments made by bots to try to get links back to their website through a link in the commenter name. If you don't have a way of protecting your site against comments then you could quickly build up a list of fake comments on your blog, which doesn't make your site look professional. With the new Google updates your website will be penalised if you are linking to bad websites, this means you need to careful when linking to another website. The best to protect your blog from comment spam is to install the Wordpress plugin akismet.com/, this will check all your comments and see if it is a spam comment or not.

Manual Comment Spam

Akismet is a great plugin to protect your blog against automated spam but it can not protect against manual spam comments. These are humans which go on to your website and comment on as many posts as possible to get links back to their site. Most of these comments will be something along the lines of "nice post" or "great article" or "thanks for sharing". One way of protecting against manual comment spam is to remove the link around the commenter name, if the commenter can see they don't get a link back most will not leave a comment. I tried this here on Paulund and the standard of intelligent comments has massively increased, now I know every comment is there to add value to the article. You can remove the URL from the comment section by using the following snippet.

function remove_comment_url_fields($fields) {
    if(isset($fields['url']))
    {
         unset($fields['url']);
    }
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_url_fields');

Time For The Author

Another problem with comments, which is infact a good problem to have is too many comments. If your blog has too many comments you need to make the choice of moderating the comments or not. If you don't moderate the comments then you can find people take advantage of this and you get more manual spam comments. But if you start to moderate the comments then you could find it takes up valuable time moderating the comments which could be spent creating more awesome content. You need to make a decision, are comments more important than spending more time on creating content. For some people they are more important but others would argue that they would prefer to spend more time on creating awesome content and allow people a different way to discuss the article.

Some of biggest blogs in the world that have turned off blog comments are:

What Are Your Options For Wordpress Coments

Even though Wordpress comes with it's own comment system which is more than capable of handling all the comments on your blog there are other options to use in it's place. Some of the most popular comments are: - Disqus

  • Livefyre
  • Facebook Comments
  • Twitter Conversations

Displaying Default Wordpress Comments

The problem with using a third party commenting system is that you do not have full control over what happens with your comments and do not have full control over what comments are published on your site. If you don't want to use a third party commenting system then you can just use the built in Wordpress comment system which will allow you to do any thing you would want from comments. To use the Wordpress comments all you have to do is create a new file called comments.php, this is where we create the HTML to display the comment form and display all the comments on the post. Once the comments.php file is created go to the single.php file and where you want to the comments to be displayed add the function comments_template();


<?php
	comments_template('', true);
?>

This function takes two parameters a filename and a boolean to decide if we will separate comments.

Displaying All Comments For the Post

Inside the comments.php file will be where you display the comments for the post, which is done by using the Wordpress function wp_list_comments() and pass in the type of comments we want to display. The type of comments will either be trackbacks or comments.


<?php if ( have_comments() ) : 
         if ( ! empty($comments_by_type['comment']) ) : ?>
				
	<ol class="commentlist">
        <?php wp_list_comments('type=comment'); ?>
        </ol>

        <?php endif; ?>
<?php endif; ?>

wp_list_comments takes a number of different arguements to make sure you get exactly the information you want.


<?php $args = array(
    'walker'            => null,
    'max_depth'         => ,
    'style'             => 'ul',
    'callback'          => null,
    'end-callback'      => null,
    'type'              => 'all',
    'page'              => ,
    'per_page'          => ,
    'avatar_size'       => 32,
    'reverse_top_level' => null,
    'reverse_children'  =>  ); 

wp_list_comments($args);
?>

Customising The Default Wordpress Comments

The wp_list_comments function is a great way to display the comments but you don't have any control on the HTML which is displayed from the function you just have to accept what it returns, so if you want to remove the link around the commenter name you are not able to. This is why this function comes will a parameter callback which allows you to define a function to display the comments which is called like this.


<ol class="commentlist">
        <?php wp_list_comments('type=comment&callback=display_custom_comments'); ?>
</ol>

Wordpress will now look for a function called display_custom_comments, now we can create this function to display each of the comments. Add the following in your functions.php file to display each comment.

function display_custom_comments($comment, $args, $depth) {
    $isByAuthor = false;

    if($comment->comment_author_email == get_the_author_meta('email')) {
        $isByAuthor = true;
    }

   $GLOBALS['comment'] = $comment; ?>

   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>" <?php if($isByAuthor){ echo 'class="author"';}?>>
      <div class="comment-author vcard">
         <?php echo get_avatar( $comment->comment_author_email, 52 ); ?>

         <?php printf(__('<cite class="comment-name">%s</cite>'), get_comment_author()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?>
      </div>
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

      <?php comment_text() ?>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
    </li>
&lt;?php
}

There are a few functions in this code snippet which need explaining which I will do so below.

Highlight Author Comments

First off we are going to add an extra CSS class to the authors comments so people can easily see these differently to the other commenter's. To find out if this is a comment from the author we compare the author email address with the commenter's email address and then we can set a variable to true to add the CSS class later.

$isByAuthor = false;

if($comment->comment_author_email == get_the_author_meta('email')) {
    $isByAuthor = true;
}

Now we will know if this is the author commenting we can add the CSS class to the outer comment DIV.


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

All the authors comments will have a CSS class of author in our style.css file we can now make sure we style this differently by adding a background color to highlight these comments. ### Get Avatar Of Commenter

Avatars are a great way of carrying your identity around with you over the internet, you set your default avatar image and this can be used by any websites you comment on. To display the avatars of your commenter's use the following snippet.


<?php echo get_avatar( $comment->comment_author_email, 52 ); ?>

The parameters you need to pass in is the email you want to get the avatar for and the size you want the avatar to be. ### Display Name Of The Commenter

If you want to just display the name of the commenter and not link to their website then you can do this by using the Wordpress function get_comment_author().

<?php printf(__('<cite class="comment-name">%s</cite>'), get_comment_author()) ?>

If you want to add a link around your commenter's name with a link back to their website then this is done by using the Wordpress function get_comment_author_link().

<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>

Add Moderator Edit Link On Comment

There will be times when you are reading a post and you scroll to the comments section and see a comment with a misspelt word or a URL included which is wrong and you need to edit the comment. You can display a quick link to edit the comment but it will only appear if you are the moderator. To add the link use the Wordpress function edit_comment_link().


<?php edit_comment_link(__('(Edit)'),'  ','') ?>

Add Reply Link To Comments

One link that must not be missed off in the comments is the reply link, making it easy for other people to reply to a certain comment. In Wordpress there is a function that will handle this for you all you have to do is use the Wordpress function comment_reply_link().


<div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>

Add No Follow To Reply Link

I noticed a problem with the reply link and this was search engines will follow the link and will index the page it goes to. All the link does is add a hash to the end of the current URL so the reply link will navigate to the same page, if this gets indexed then you will have duplicate entries in the search results. A good way to stop search engines going into these links is to add a no follow on the reply links to do this you need to add the following to your functions.php file.


/**
* Add a rel="nofollow" to the comment reply links
*/
function add_nofollow_to_reply_link( $link ) {
    return str_replace( '")\'>', '")\' rel=\'nofollow\'>', $link );
}
add_filter( 'comment_reply_link', 'add_nofollow_to_reply_link' );

Stop Autolinks In Comments

When a commenter places a link in there comment WordPress will search through this text find that URL and automatically turn this into a link. But what if the comment is a spam comment then this will automatically link back to the spamming website. This is bad for SEO if you are seen to be linking back to spamming websites then Google can treat your website as spam too, or at least it will put your website down in the search results. To turn this functionality off and stop autolinking URLs in comments just add the following code to your functions.php file.


remove_filter('comment_text', 'make_clickable', 9);

Automatically Spam Long URLs

When you get spam comments in WordPress most of the time the spam bot is working to build links to some of the pages deep in a website, but when normal people comment they normally link back to the homepage of their website. This means that you can create a rule to automatically set comments to spam if the commenter URL is longer than a certain number of characters. Here is a WordPress snippet you can place in the functions.php file to automatically set comments with long URLs as spam.


function auto_spam_long_urls( $approved , $commentdata ) {
    $spamLength = 75;

    return ( strlen( $commentdata['comment_author_url'] ) > $spamLength ) ? 'spam' : $approved;
}

add_filter( 'pre_comment_approved', 'auto_spam_long_urls', 99, 2 );

Block External Comment Form Posting

The comment form on a WordPress website will post to wp-comments-post.php, therefore if I know a website is running on WordPress I can post a comment on the website by creating a form and making sure the post is going to http://www.example.com/wp-comments-post.php. To stop comment spam bots you can add something in the htaccess file to stop bots posting to form from outside the website.


RewriteEngine On
RewriteCond %{REQUEST_URI} /(comments-post|setup)\.php$
RewriteCond %{HTTP_REFERER} !.*YOURDOMAIN.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

Conclusion

Now you understand what is involved in comments on your blog you have a couple of choices to make with the comments on your blog. Do you display comments at all? Do you use the default Wordpress comments engine or a third party comments system? Do you move comments over to social media where people go to engage?

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring