Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to hack the default Recent Comments widget to show also the avatar of commenter.

The modification should be really easy, if I change directly the code in default-widgets.php

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>') . get_avatar($comment->user_id , 20) .  '</li>';
    }
}

but I don't like change core files and I would prefer a more ortodox way to implement it.

How can I do it?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Sounds like you just need to make another widget of your own in a plugin. So copy the complete code from the default widgets file, and change the class name, then just edit the code you'd like to edit.

 class YOUR NEW WIDGET NAME extends WP_Widget {
       // ...

        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>';
            }

    // ...

}

Then you just register the new widget:

add_action( 'widgets_init', 'register_my_widget' );
function register_my_widget() {
     register_widget ( YOUR NEW WIDGET NAME );
}

All that should probably go inside a plugin so you don't loose your widget when changing themes.

More info:

http://xavisys.com/wordpress-widget/

http://codex.wordpress.org/Writing_a_Plugin

http://codex.wordpress.org/Widgets_API

share|improve this answer

You can copy the widget code and make your own plugin with the changes you want or, alternatively, use one of the existing plugins that already do this. For example: http://wordpress.org/extend/plugins/bwp-recent-comments/

share|improve this answer

protected by toscho Sep 16 '12 at 13:41

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.