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);
}
?>
Show me the original version
Hide this version
Original plain text version can be downloaded here: here
<?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 = '/mini';
} else if ( $twav_suffix < 64 ) {
$twav_suffix = '';
} else {
$twav_suffix = '/bigger';
}
} else if ( 'full' == $id_or_email['type'] ) {
$twav_size = BP_AVATAR_FULL_WIDTH;
$twav_suffix = '/bigger';
} else if ( 'thumb' == $id_or_email['type'] ) {
$twav_size = BP_AVATAR_THUMB_WIDTH;
}
$out = 'http://purl.org/net/spiurl/'. 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.