5 Funky WordPress Snippets to Customize Your Comments

First the phrase was “there’s an app for that”. Now, the prevailing sentiment is “there’s a plugin for that” — which isn’t always a good thing. Many of the customizations you envision for your WordPress comments section can easily be made in your theme files, so you can get the polished comments section you want without bloating your site with even more plugins.

Not a programmer? You don’t have to be. Below, you’ll find simple codes with clear customization instructions. If you’re having problems, check out the tips for suggestions on how to correct them.
Please note that this was written for the Twenty Eleven theme, so if your site is based on another theme, some file names may be different.

Display the Number of Comments/Trackbacks

Most templates begin their comments section with a message on how many comments there are. But what if you also want to display a comment tally somewhere else on the post, like the top? This code snippet lets you place a comment tally and message anywhere in the single page loop, and customize which message comes back for zero, one, and multiple responses.

Template file:

content-single.php

Raw Code:

<?php comments_number( 'no responses', 'one response', '% responses' ); ?>.

How to Use it:

The raw code shows the most basic messages to display for zero, one, and multiple messages. The following example shows the message wrapped in a p tag with a short message (“Join the discussion!”) preceding the dynamic text. Simply switch out the raw code messages for what you want to say. Perhaps you want to be more conversational, like below:

<p> Join the discussion! <?php comments_number( 'You get to have the first comment!', 'One comment so far - what can you add?', '% people have commented - add your two cents!' ); ?> </p> 

Example output:

no comments: “You get to have the first comment!”

one comment: “One comment so far – what can you add?”

multiple comments: “3 people have commented – add your two cents!”

Tips:

  • If the code breaks your page, check to see if you accidentally put apostrophes in the string text. If so, use the following HTML code to display apostrophes: &rsquo;
  • Building on this knowledge, you can easily tweak the way your comments section displays its text. Just open your comments.php file and search for the text that is currently displaying (default for TwentyEleven begins with “One thought on…”, and replace the string text with your own.

Learn more about this code:

http://codex.wordpress.org/Function_Reference/comments_number

Customize Your Default Avatar

Many blog visitors aren’t registered on Gravatar, so their avatar will show whatever your default is set as. If your comments section needs a little more personality than the ever-present Mystery Man, why not create one unique to your site?

Template file:

functions.php

Raw Code:

add_filter( 'avatar_defaults', 'new_default_avatar' ); function new_default_avatar ( $avatar_defaults ) { $new_avatar_url = get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; $avatar_defaults[$new_avatar_url] = 'My Custom Avatar'; return $avatar_defaults; }

How to Use it:

Create your image at 60px by 60px, and upload it to the images folder in your theme.

In the code, replace the image path and file name (‘/images/new_default_avatar.png’) with your own. If you want to name your default avatar, you can do so by replacing “Your New Default Avatar” with your chosen name. This name will only show up in your settings page, which we’re about to go to.

On your Dashboard, go to Settings > Discussion, where you should see your new custom avatar. Select it, click Save, and enjoy!

Tips:

  • If you must place your image somewhere other than the main template directory, or for some reason it won’t link properly, try replacing get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; with the full (http:// and all) URL leading to that image.
  • If you’d like to style the avatar with CSS, open your stylesheet and add a style for:  .commentlist .avatar {}

Learn more about this code:

http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress

Remove URL or Other Fields from Contact Form

The default fields in most contact forms are Name, Email, Website, and Comment. While all of those fields have excellent reasons for being there, some would prefer to leave out certain ones — most famously the website field, which spammers often try to take advantage of.

This easy amendment to your functions.php file will take care of those unwanted fields in a flash!

Template file:

functions.php

Raw Code:

add_filter('comment_form_default_fields', 'remove_url'); function remove_url($val) { $val['url'] = ''; return $val; }

How to use it:

This code, pasted as is, will remove the website field. Other fields can be removed by replacing ‘url’ with ‘email’ or ‘author’ (name) in all three places it exists in the code.

Learn more about this code:

http://codex.wordpress.org/Function_Reference/comment_form

Keep Your Blog from Pinging Itself

It’s nice to get trackbacks from sites that have linked to your article. It’s not nice to get pinged every time you link within your own website. Before you reach for that plugin, try adding this simple snippet to your functions.

Template file:

functions.php

Raw Code:

function disable_self_ping( &$links ) { foreach ( $links as $l => $link ) if ( 0 === strpos( $link, get_option( 'home' ) ) ) unset($links[$l]); } add_action( 'pre_ping', 'disable_self_ping' );

How to Use it:

Simply paste the code at the end of your functions.php file. It doesn’t get much simpler!

Learn more about this code:

http://wp-snippets.com/disable-self-trackbacks/

Change “Leave a Reply” Text

If “Leave a Reply” sounds little cold and impersonal to you, you might consider changing it up with this code:

Template file:

comments.php

Raw Code:

<?php comment_form(array('title_reply'=>'Put in Your Two Cents')); ?>

How to Use it:

In your theme folder’s comments.php file, search for the <?php comment_form(); ?> line close to the bottom. Replace it with the raw code above.

Now, simply change out ‘Put in Your Two Cents’ with whatever text you want to show there.

Learn more about this code:

http://wp-snippets.com/disable-self-trackbacks/

Have fun with these snippets; hopefully they give your comments area that extra zing!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Dan

    An apostrophe should not be replaced with ’ which is a right single quote, it should be replaced with '

  • Dan

    The codes for my comment above being

    ’ and '
  • Andrew

    I’m just getting into WordPress but was under the impression that when making mods it was almost always better to create a child theme so that you can still update the master / parent theme?

    I’m also aware that the functions.php file is loaded from the child theme first and then the parent theme with the thinking being you can add new functions but leave the old ones in tact and reference them in your child functions.php file? That been the case how would you implement any changes above that involve modifying the functions.php file – would you have to manually change it every time you updated the parent theme?

    • sandra

      @Andrew

      It’s actually the other way round: the parent theme gets loaded **before** the child theme: otherwise, programmatically you would not be able to inherit and reference specific functions only defined in the parent theme. (In fact it is a little more complicated because built-in priorities enable you to trigger child theme’s hooks and filters functions before or after parent’s hooks and filters functions as required by the process you would want to render).

      Now coming to the snippets documented in this article, in you case, they are to be used in the child theme. This way if you upgrade your parent theme to the newest version, you do not lose the benefits of your snippets.

      Now you could also use them in any theme, if you are writing one from scratch that does not rely on inheriting a parent theme.