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.

  1. if (!has_filter ('get_avatar', 'tweetimport_display_avatar')) {add_filter('get_avatar', 'tweetimport_display_avatar');}
  2. if (!function_exists('tweetimport_display_avatar')):
  3. function tweetimport_display_avatar($avatar)
  4. {
  5. global $post;
  6. global $in_comment_loop;
  7. if (in_the_loop() && !$in_comment_loop):
  8. $twitter_avatar = get_post_meta($post->ID,'tweetimport_author_avatar' , true);
  9. if (!isset($twitter_avatar) || $twitter_avatar == '')
  10. return $avatar;
  11. return preg_replace ('/<img(.*?)src=["|\'](.*?)["|\'](.*?)>/i', '<img$1src="'. $twitter_avatar . '"$3>', $avatar);
  12. else :
  13. return $avatar;
  14. endif;
  15. }
  16. 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.