Separating WordPress comments from pings and trackbacks

Separating comments and pings

There are many techniques that can be employed to separate comments and ping and one of the simplest method is to go through the $comments variable before the comments are displayed, and manually separate them. Let’s look at a routine to do this:

<?php
 
function ping_comments ($comments)
{
  global $pings, $comment;
 
  // Initialise the variables
  $pings = $newcomments = array ();
 
  // Loop through existing comments
  foreach ($comments AS $comment)
  {
    if (get_comment_type () == 'comment')
      $newcomments[] = $comment;
    else
      $pings[] = $comment;
  }
 
  // Return the comments without any pings
  return $newcomments;
}
 
// Adjust comments number so comments_number() function is correct
function ping_comments_number ($num)
{
  global $pings;
  return $num - count ($pings);
}
 
// Hook into WordPress filters
add_filter ('get_comments_number', 'ping_comments_number');
add_filter ('comments_array', 'ping_comments');
 
?>

It’s not too important to understand exactly how this works. The code basically hooks into WordPress, filtering out pings from the comments and putting them in a separate variable called $pings. The code also ensures that the total number of comments is adjusted to not include the number of pings (not doing this would give us incorrect count values).

This code can be inserted into functions.php in the theme directory, which will ensure it runs everytime a page is displayed. An alternative is to download it as a plugin, which is a much simpler method.

14 thoughts on “Separating WordPress comments from pings and trackbacks”

  1. Hi,
    I love the concept of this plugin because it cleans things up really nicely. However, is there an easy way to aggregate the pings in one place so you can keep the conversation easy and flowing, yet as a blogger be able to see easily (without visiting every post) who has pinged your site?

    Thanks!

  2. You would need a plugin to provide that functionality. It wouldn’t be difficult to implement, and just requires querying the database to fetch all pings.

  3. JHS, it is because you put the code to display the pings after the comment form. In comments.php just moved the pings code above the part that starts ‘leave a comment’.

  4. Hey there, this is a great page. How do you change the code so it says "1 ping" rather than "1 pings" if that’s the case? Also, ever since following your directions, the no self-ping plugin doesn’t work.

  5. Jessica, you can use something like:

    <?php echo sprintf (__ngettext ('%d post', '%d posts', $pings), $pings); ?>

    This will output '1 ping' or '2 pings'

  6. i’ve activated the separate-comments-pings plugin and have replaced my comments.php file with comments.php file (taken from default folder/zip, i download from this site). but i got error:

    Parse error: syntax error, unexpected T_ENDIF in /home/aufkgorg/public_html/wp-content/themes/vista/comments.php on line 144

    so, please give solution !

  7. Kevin, I had simmilar problem, find last and delete one

    John, I changed line from your previous comment, but it gives “1 posts”. Can you, please, check it?

  8. Hello there,

    First off thanks for a great plugin!

    Still I have a question. How did you get your pings and trackbacks so nice? I have used the piece of code you provided:


    foreach ($pings AS $comment)
    $display[] = sprintf ('%s',$comment->comment_author_url,$comment->comment_content); echo implode (', ', $display);?>

    But im still getting something that looks like this: […] Lets make our move… […]
    […] Lets make our move… […]

    How did you get yours so nicely like above: techathand.net, techie-buzz.com
    Just the main website name and it links to the specific article.

    Thanks in advance 🙂

  9. Now I’d love to know the code on how to separate pings from comments using WordPress version 2.7… 🙂 (i.e., when the threaded and paged comments functions are activated)