I previously described how to display Twitter author avatar when using Tweet Import which detailed how to edit your template to include the Twitter user image URL added as a custom field for imported tweets.

But in case you are using a theme such as P2, and would like to change the Avatar displayed without editing the template files, below is a hack that allows you to do that.

To achieve this, please add the following to your theme “functions.php” file.

if (!has_filter ('get_avatar', 'tweetimport_display_avatar')) {add_filter('get_avatar', 'tweetimport_display_avatar');}
if (!function_exists('tweetimport_display_avatar')):
function tweetimport_display_avatar($avatar)
{
  global $post;
  global $in_comment_loop;
  if (in_the_loop() && !$in_comment_loop):
    $twitter_avatar = get_post_meta($post->ID,'tweetimport_author_avatar' , true);
    if (!isset($twitter_avatar) || $twitter_avatar == '')
      return $avatar;
    return preg_replace ('/<img(.*?)src=["|\'](.*?)["|\'](.*?)>/i', '<img$1src="'. $twitter_avatar . '"$3>', $avatar);
  else :
    return $avatar;
  endif;
}
endif; //tweetimport_display_avatar

This code just applies a custom ‘get_avatar’ filter that substitutes the avatar URL generated by WordPress with the ‘tweetimport_author_avatar’ which is a custom field created by Tweet Import and whose value is the URL to the Twitter User image.