1

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?

4

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

1

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/

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