post

How to Remove Pingbacks from the WordPress Recent Comments Widget

There’s nothing more annoying than pingbacks displaying in the WordPress Recent Comments Widget. This week I set out on a quest to discover how to remove them from the widget. I’d already discovered how to have have them not display on the comments page, but the widget is different altogether and the fix is so simple that I could kick myself. The sourcecode for the recent comments widget can be found in the wp-includes/default-widgets.php file. Search for the line that says:

  1. $comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) );
$comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) );

and change it to

  1. $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'type' => 'comment' ) );
$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'type' => 'comment' ) );

That’s all it takes to filter the pingbacks out. Of course, you really don’t want to mess around with the default WordPress widgets. Any WordPress update will overwrite your changes. What I did was copy the default Recent Comments source code and added to the bottom of my theme’s functions.php. The new code is below. It has been modified so it does not conflict with the deafult widget.

  1. class VMF_Widget_Recent_Comments extends WP_Widget {
  2.  
  3.     function VMF_Widget_Recent_Comments() {
  4.         $widget_ops = array('classname' => 'vmf_widget_recent_comments', 'description' => __( 'The most recent comments' ) );
  5.         $this->WP_Widget('vmf-recent-comments', __('VMF Recent Comments'), $widget_ops);
  6.         $this->alt_option_name = 'vmf_widget_recent_comments';
  7.  
  8.         if ( is_active_widget(false, false, $this->id_base) )
  9.             add_action( 'wp_head', array(&$this, 'vmf_recent_comments_style') );
  10.  
  11.         add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
  12.         add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
  13.     }
  14.  
  15.     function vmf_recent_comments_style() {
  16.  ?>
  17.     <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
  18. <?php
  19.     }
  20.  
  21.     function flush_widget_cache() {
  22.         wp_cache_delete('vmf_widget_recent_comments', 'widget');
  23.     }
  24.  
  25.     function widget( $args, $instance ) {
  26.         global $comments, $comment;
  27.  
  28.         $cache = wp_cache_get('vmf_widget_recent_comments', 'widget');
  29.  
  30.         if ( ! is_array( $cache ) )
  31.             $cache = array();
  32.  
  33.         if ( isset( $cache[$args['widget_id']] ) ) {
  34.             echo $cache[$args['widget_id']];
  35.             return;
  36.         }
  37.  
  38.         extract($args, EXTR_SKIP);
  39.         $output = '';
  40.         $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);
  41.  
  42.         if ( ! $number = (int) $instance['number'] )
  43.             $number = 5;
  44.         else if ( $number < 1 )
  45.             $number = 1;
  46.  
  47.         $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'type' => 'comment' ) );
  48.         $output .= $before_widget;
  49.         if ( $title )
  50.             $output .= $before_title . $title . $after_title;
  51.  
  52.         $output .= '<ul id="recentcomments">';
  53.         if ( $comments ) {
  54.             foreach ( (array) $comments as $comment) {
  55.                 $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
  56.             }
  57.         }
  58.         $output .= '</ul>';
  59.         $output .= $after_widget;
  60.  
  61.         echo $output;
  62.         $cache[$args['widget_id']] = $output;
  63.         wp_cache_set('vmf_widget_recent_comments', $cache, 'widget');
  64.     }
  65.  
  66.     function update( $new_instance, $old_instance ) {
  67.         $instance = $old_instance;
  68.         $instance['title'] = strip_tags($new_instance['title']);
  69.         $instance['number'] = (int) $new_instance['number'];
  70.         $this->flush_widget_cache();
  71.  
  72.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  73.         if ( isset($alloptions['vmf_widget_recent_comments']) )
  74.             delete_option('vmf_widget_recent_comments');
  75.  
  76.         return $instance;
  77.     }
  78.  
  79.     function form( $instance ) {
  80.         $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  81.         $number = isset($instance['number']) ? absint($instance['number']) : 5;
  82. ?>
  83.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  84.         <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  85.  
  86.         <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  87.         <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  88. <?php
  89.     }
  90. }
  91.  
  92. // register VMF_Widget_Recent_Comments widget
  93. register_widget('VMF_Widget_Recent_Comments');
class VMF_Widget_Recent_Comments extends WP_Widget {

	function VMF_Widget_Recent_Comments() {
		$widget_ops = array('classname' => 'vmf_widget_recent_comments', 'description' => __( 'The most recent comments' ) );
		$this->WP_Widget('vmf-recent-comments', __('VMF Recent Comments'), $widget_ops);
		$this->alt_option_name = 'vmf_widget_recent_comments';

		if ( is_active_widget(false, false, $this->id_base) )
			add_action( 'wp_head', array(&$this, 'vmf_recent_comments_style') );

		add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
		add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
	}

	function vmf_recent_comments_style() {
 ?>
	<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
<?php
	}

	function flush_widget_cache() {
		wp_cache_delete('vmf_widget_recent_comments', 'widget');
	}

	function widget( $args, $instance ) {
		global $comments, $comment;

		$cache = wp_cache_get('vmf_widget_recent_comments', 'widget');

		if ( ! is_array( $cache ) )
			$cache = array();

		if ( isset( $cache[$args['widget_id']] ) ) {
			echo $cache[$args['widget_id']];
			return;
		}

 		extract($args, EXTR_SKIP);
 		$output = '';
 		$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);

		if ( ! $number = (int) $instance['number'] )
 			$number = 5;
 		else if ( $number < 1 )
 			$number = 1;

		$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'type' => 'comment' ) );
		$output .= $before_widget;
		if ( $title )
			$output .= $before_title . $title . $after_title;

		$output .= '<ul id="recentcomments">';
		if ( $comments ) {
			foreach ( (array) $comments as $comment) {
				$output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
			}
 		}
		$output .= '</ul>';
		$output .= $after_widget;

		echo $output;
		$cache[$args['widget_id']] = $output;
		wp_cache_set('vmf_widget_recent_comments', $cache, 'widget');
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = (int) $new_instance['number'];
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['vmf_widget_recent_comments']) )
			delete_option('vmf_widget_recent_comments');

		return $instance;
	}

	function form( $instance ) {
		$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
		$number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>

		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
	}
}

// register VMF_Widget_Recent_Comments widget
register_widget('VMF_Widget_Recent_Comments');

VictorFont.com runs on the Genesis Framework

Genesis FrameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Check out the incredible features and the selection of designs. It's that simple—start using Genesis now!

Click here to download The Genesis Guide for Absolute Beginners (PDF - 1.4 MB)

Comments

  1. says

    Three years later your simple tweak still works like a charm. I can’t believe this issue hasn’t been fixed by WP, there are so many frustrated threads about it, but this was the only solution I found. Thank you.

  2. Vernon says

    Mahalo Victor for sharing this method of removing the pingbacks. I have been trying for several months to figure this out and your tip worked like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

Current day month ye@r *