Separating WordPress comments from pings and trackbacks

Aug 9, 2007 | Tags: , , , , | Written by Administrator

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.

Please direct all support questions to the support forum.

Share This

Comments

Comments are shown on the first page.

Home | Software | Terms & Conditions | Sitemap | John Godley © 2008
Close
E-mail It