First up, the first step in this process is to install the Twit Connect plugin which you can do by following these instructions, then you can proceed with enabling Twitter avatars.
This is just a quick filter that I wrote while I was setting up help.deanjrobinson.com last week after I discovered that the twitter avatars weren’t working because of a different function/filter being used by BuddyPress.
I make no claims that this additional function is perfect, but I haven’t come across any problems with it yet, and I only knocked it together quickly. If you find anything wrong with it, or have any suggestions as to how it could be improved/simplified please let me know.
This new function based on one of the original functions that comes with the Twit Connect plugin which enables twitter avatars on regular WordPress blogs, just a with a few tweaks.
What you need to do is add the following code to the functions.php in your current BuddyPress theme. You can download a plain text copy of this function here (it’ll probably more reliable than copy-and-pasting from this page). Remember to backup your functions.php file before making any changes… just in case.
UPDATE: Appears some people who added this function the functions.php file in the default BuddyPress theme were seeing some error messages (I have not yet been able to recreate this error, but more than one user has reported it). Ideally you shouldn’t be modifying the default BP theme directly, you should be doing it via a child theme (find out how), so this code should be then added to the functions.php in your child theme.
7th March, UPDATE I’ve updated the function below (and the downloadable version above) to use http://tweetimag.es for the static twitter avatar URLs, it looks like it might a more reliable/consistent service.
<?php
function bp_twc_get_avatar($avatar, $id_or_email='') {
global $comment, $twc_user_login_suffix;
if(is_object($comment)) {
$user_id = $comment->user_id;
}
if (is_object($id_or_email)) {
$user_id = $id_or_email->user_id;
}
if (is_array($id_or_email)) {
if ($id_or_email['object']=='user') {
$user_id = $id_or_email['item_id'];
}
}
if (get_usermeta($user_id, 'twcid')) {
$user_info = get_userdata($user_id);
$twav_suffix = '';
if ( $id_or_email['width'] ) {
$twav_size = $id_or_email['width'];
if( $twav_size < 32 ) {
$twav_suffix = 'm';
} else if ( $twav_suffix < 64 ) {
$twav_suffix = 'n';
} else {
$twav_suffix = 'b';
}
} else if ( 'full' == $id_or_email['type'] ) {
$twav_size = BP_AVATAR_FULL_WIDTH;
$twav_suffix = 'b';
} else if ( 'thumb' == $id_or_email['type'] ) {
$twav_size = BP_AVATAR_THUMB_WIDTH;
$twav_suffix = 'n';
}
$out = 'http://img.tweetimag.es/i/'. str_replace($twc_user_login_suffix,"",$user_info->user_login) . '_' .$twav_suffix;
$avatar = "<img alt='Twitter Avatar' src='{$out}' class='avatar avatar-{$twav_size}' height='{$twav_size}' width='{$twav_size}' />";
return $avatar;
} else {
return $avatar;
}
}
// Check if Twit Connect exists (since its without it this function is pointless)
if( function_exists( 'twit_connect' ) ) {
add_filter( 'bp_core_fetch_avatar', 'bp_twc_get_avatar',10,4);
}
?>
I will add an explanation of what its doing if people are interested/curious.
So how do you call the avatar in a WordPress template?
Cheers Dean, I have been looking for a solution all Sunday. Yours came right in time!!!
Hi Dean, just discovered same error modemlooper had. I am pretty sure I took the right functions.php. When I choose edit profile or log out I get the same error. Hope you can help:
Warning: Cannot modify header information — headers already sent by (output started at /domains/pixelcasting.com/public_html/moestuintwt/wp-content/plugins/buddypress/bp-themes/bp-default/functions.php:3) in /domains/pixelcasting.com/public_html/moestuintwt/wp-includes/pluggable.php on line 868
More info: I’m running Single WP 2.9.1, BP 1.2.1 and Twit Connect is installed
Having a bit of a look now to see if I can spot a possible cause, I’ve tried the things you’ve mentioned but haven’t been able to replicate the issue thus far.
I had to use a child theme function file. If it was placed in the default functions file it threw up that error.
That’s it modemlooper! I made a simple child theme: inherit style.css and a functions.php with Dean’s code only. Uploaded those to wp-content/themes/childthemetwitavatar/ and activated the child theme in admin. See the description on how to build a child theme on: http://codex.buddypress.org/how-to-guides/building-a-buddypress-child-theme/. Dean, send me a DM with your email address @volkstuin and I will mail you the files, so you can add it here for other users, if you like.
Cheers guys!!
Thanks for the info, I’ve added a note to the post mentioning that this function is best added to a child theme, not directly to the core default theme.
Tried this on single WP and it threw up header errors.
I’m assuming that both the BuddyPress 1.2 and TwitConnect plugins are installed and activated, can you post the specific error your getting and I’ll see if I can help.
a test site. http://tempsg.com/buddypress/
ok I am going cross eyed. LOL I had different functions file open from different sites and pasted the code into the wrong site. Working now perfectly.
heh, I do that sort of thing all the time, glad to hear its all working now.
avatars have died and the img src is http://purl.org/net/spiurl/modemlooper When you go to that url it say google app engine has died.
lol, guess that means there are too many people using Twit Connect (the ‘app’ that is used by Twit Connect to retrieve the static twitter avatars is, as far as I know, run by the author of the Twit Connect plugin)… he’ll probably need to find a different method of referencing twitter profile images (static urls for twitter avatars should be part of the twitter api but as far as I am aware it is not)
@modemlooper I’ve just updated the function to use a different service for retrieving static twitter avatars which looks like it might be more reliable, it uses the tweetimag.es ( eg: http://img.tweetimag.es/i/modemlooper_n ) all you should need to do is update the bp_twc_get_avatar function with the new version I’ve added to this post and it should be ready to go.
Sorry my fault was a janky file. Though I added this it did not pull in the avatar.
massive thanks Dean for taking the time out to write this and offer it, kudos dude!