RSS 2.0 Follow Us!

Related Posts

WordPress Tech: Upgrading the Default Recent Comment Widget

John on December 22, 2009 at 12:17 pm

I like widgets for the sidebar. They’re handy. But the default “recent comments” widget that comes with WordPress (up to version 2.9 now) has some problems. The main one is that it displays the entire user name, followed by “on,” followed by the entire post title.

Also, by default, both the user name and post title are links. The username links to his or her website and the post title to the post. This tends to be a big mess, especially if the user name is actually a recent trackback and the post title was longish. You might get something like this:

Tweets that mention This is One of the Greatest Posts Ever on This is One of the Greatest Posts Ever

Line up five or seven of those in a row and you have a real mess in your sidebar. Unfortunately, since this is a default widget, the code is part of the core of WordPress and it’s neither easy to locate or all that easy to fix unless you’re pretty handy with PHP.

After spending a little time on this yesterday, I figure I might as well share my solution with everyone. This is a hack, so if you’re not comfortable hacking the code, well, don’t try this at home. However, there is an easy way to do this and that’s just to copy my revised version of the file, which you can download here. Note that I’ve changed the extension to .txt. Just rename it to .php and upload it to your wp-includes directory.

Now, for those who want to see what’s going on, here are the details:

  • The default widget functions are located here: wp-includes/default-widgets.php
  • In WordPress 2.9 the recent comments  function begins on line 596.
  • Scroll down just a bit more to line 646, which should look like this:
    if ( $comments ) : foreach ( (array) $comments as $comment) :
  • Immediately after that line I’ve added a big block of code. What this block does is grab the user name and post title for recent comments, however it shortens each of them before sending them to the screen. Best of all it does this in a word sensitive manner so you don’t get any partial words in your sidebar.
  • In the code below you’ll see two variables called $len and $len2. These set the length of each piece of the recent comment. $len sets the length (in characters) of the post title. $len2 sets the length of the user name. You can set your own lengths or go with my defaults.
  • This code (which I borrowed from will give you a shorter, cleaner list of recent comments without a lot of word fragments. I’ve also removed the user name link from the output so it isn’t such a mess.
  • Okay, here’s the code:

[sourcecode language='php']// Here is where I introduce a hack to shorten the length of the post title in a word sensitive manner
$myhack = get_the_title($comment->comment_post_ID);
// This next line sets the length in characters
$len = 50;
if( (mb_strlen($myhack) > $len) ) {

$whitespaceposition = mb_strpos($myhack,” “,$len)-1;
if( $whitespaceposition > 0 ) {
$chars = count_chars(mb_substr($myhack, 0, ($whitespaceposition+1)), 1);
if ($chars[ord('<')] > $chars[ord('>')])
$whitespaceposition = mb_strpos($myhack,”>”,$whitespaceposition)-1;
$myhack = mb_substr($myhack, 0, ($whitespaceposition+1));
// close unclosed html tags
if( preg_match_all(“|<([a-zA-Z]+)|",$myhack,$aBuffer) ) {

if( !empty($aBuffer[1]) ) {


if( count($aBuffer[1]) != count($aBuffer2[1]) ) {

foreach( $aBuffer[1] as $index => $tag ) {

if( empty($aBuffer2[1][$index]) || $aBuffer2[1][$index] != $tag)
$myhack .= ‘‘;
// Now we need to do the same for the author, first remove the links
$myhack_author = strip_tags (get_comment_author_link() );
// Now set the max length of the author name
$len2 = 8;

if( (mb_strlen($myhack_author) > $len2) ) {

$whitespaceposition = mb_strpos($myhack_author,” “,$len2)-1;
if( $whitespaceposition > 0 ) {
$chars = count_chars(mb_substr($myhack_author, 0, ($whitespaceposition+1)), 1);
if ($chars[ord('<')] > $chars[ord('>')])
$whitespaceposition = mb_strpos($myhack_author,”>”,$whitespaceposition)-1;
$myhack_author = mb_substr($myhack_author, 0, ($whitespaceposition+1));


Finally I made some edits to the code immediately below this block to include my new variables. It looks like this:

[sourcecode language='php']echo  ‘

  • ‘ . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x(‘%1$s on %2$s’, ‘widgets’), $myhack_author , ‘

    Post to Twitter

  • Category: Science & Tech |

    Sorry, the comment form is closed at this time.