How to disable Gravatar completely?
-
Hey everyone,
I’m really impressed with the avatar upload process – it’s awesome!
I want to disable Gravatars completely though. I’ve gone into the settings and set the default avatar to ‘Mystery Man’, but I’ve noticed that it’s still calling to gravatar.com for this image… so I assume it’s attempting to load each user’s gravatar from their gravatar account, if they have one (and haven’t uploaded a picture to my site)
Is it possible to remove Gravatar COMPLETELY from the equation?
Thanks!
-
For the record, I’ve got this in bp-custom.php:
/* Set default image for BP users without avatar: */<br />
define ( 'BP_AVATAR_DEFAULT', get_bloginfo('template_url') . '/_inc/images/default-full.png' );<br />
define ( 'BP_AVATAR_DEAFULT_THUMB', get_bloginfo('template_url') . '/_inc/images/default-thumb.png' );<br />Wow, I’ve tried everything now (obviously except the one thing that works…)!
Anybody got any clues? Thanks!
Ok finally fixed it with some core code hacking
Here’s my patch solution for anyone wanting to replace default avatars and disable gravatars:
in bp-core-avatars.php, function bp_core_fetch_avatar():
I replaced this:
$gravatar = apply_filters( 'bp_gravatar_url', 'https://secure.gravatar.com/avatar/' ) . md5( $grav_email ) . '?d=' . $default_grav . '&s=' . $grav_size;
with this line:
$gravatar = get_bloginfo('template_directory') . '/images/new-default.jpg';
then in bp-core-templatetags.php, function bp_get_signup_avatar():
replaced this line:
return apply_filters( 'bp_get_signup_avatar', '<img src="' . $gravatar_url . md5( $_POST['signup_email'] ) . '?d=' . $default_grav . '&s=' . $size ) . '" width="' . $size . '" height="' . $size . '" alt="' . $alt . '" class="' . $class . '" />';
with these:
$default_grav = get_bloginfo('template_directory') . '/images/new-default.jpg';
return apply_filters( 'bp_get_signup_avatar', '<img src="' . $default_grav . '"' ) . '" width="' . $size . '" height="' . $size . '" alt="' . $alt . '" class="' . $class . '" />';Also added a filter to functions.php as it was sometimes returning an image tag with an empty src (broken avatars) – it checks for the empty tag and replaces it with the default:
Note: BP_AVATAR_DEFAULT was defined in my bp-custom.php file.
function no_empty_avatar($avatar_link) {
if ( strpos( $avatar_link, "src=''" ) ) {
$avatar_link = str_replace("src=''", "src='" . BP_AVATAR_DEFAULT . "'", $avatar_link );
}
return $avatar_link;
}
add_filter( 'bp_core_fetch_avatar', 'no_empty_avatar' );Hope this helps someone in future..
If anyone has a better solution without hacking the core files, please let me know!
You shouldn’t have to edit the core files at all. I had to whip up something similar today for a client which you may find helpful.
What it does is replace the email that is used to access the gravatar with a different one. I recommend you replace the default email template with your domain, so, the gravatar will remain persistant if it’s an identicon or wavatar:
class DisableUserGravatar {
var $email_template = "member.%USER%@yourwebsite.com";
function DisableUserGravatar(){
add_filter('get_avatar', array(&$this, 'wp_avatar'), 1, 5);
add_filter( 'bp_core_fetch_avatar', array(&$this, 'bp_avatar'), 1, 3);
}
function wp_avatar( $content, $id_or_email){
if( preg_match( "/gravatar.com/", $content ) ){
if ( is_numeric($id_or_email) ) {
$id = (int) $id_or_email;
$user = get_userdata($id);
} elseif ( is_object($id_or_email) ) {
if ( !empty($id_or_email->user_id) ) {
$id = (int) $id_or_email->user_id;
$user = get_userdata($id);
} elseif ( !empty($id_or_email->comment_author_email) ) {
return $content; //Commenters not logged in don't need filtering
}
} else {
$user = get_user_by_email($id_or_email);
}
if(!$user) return $content;
$username = $user->user_login;
$email = md5( str_replace('%USER%', $username, $this->email_template) );
return preg_replace("/gravatar.com\/avatar\/(.+)\?/", "gravatar.com/avatar/{$email}?", $content);
}
return $content;
}
function bp_avatar( $content, $params ){
if( is_array($params) && $params['object'] == 'user' ){
return $this->wp_avatar($content, $params['item_id']);
}
return $content;
}
}
global $DisableUserGravatar;
$DisableUserGravatar = new DisableUserGravatar();Create a file in wp-content/mu-plugins with the content above.
I’ll put this up on wordpress.org as a plugin.
I understand the point behind standard Gravatar integration in WP & BP but what if your target audience is by no means tech-savy and has very low probability of having a Gravatar account (I myself don’t even have one…)?
Doesn’t that make multiplying http requests to display the default mistery man completely pointless? And with the integrated “avatar upload” function in Buddy press, i’m guessing anyone even remotely willing to upload an avatar will do it on site…
So can’t there be a straightforward way to just get “mistery man” directly from inside the buddy press install? As I understand it, Marcus’ plugin just avoids displaying the Gravatar but still sends out requests to gravatar.com which is not great for page speed.
I’m pretty sure a few people are concerned with this Gravatar dependency and would rather have the choice of wether or not to use the Gravatar service to display avatars.
Hi @BuddyPresser !
I have found out another way get it not editing core files. Put this filter into your functions.php file:`function bp_remove_gravatar ($image, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir) {
$default = ‘http://yoursite.net/wp-content/plugins/buddypress/bp-core/images/mystery-man.jpg’;
if( $image && strpos( $image, “gravatar.com” ) ){
return ‘‘;
}else
return $image;}
add_filter(‘bp_core_fetch_avatar’, ‘bp_remove_gravatar’, 1, 9 );function remove_gravatar ($avatar, $id_or_email, $size, $default, $alt) {
$default = ‘http://yoursite.net/wp-content/plugins/buddypress/bp-core/images/mystery-man.jpg’;
return ““;
}add_filter(‘get_avatar’, ‘remove_gravatar’, 1, 5);
`It worked for me
Hi!
I’m looking for a way to disable gravatar and keeping a different defaut avatar for group and user.
Could I put a line to do the difference or not?Hi @BuddyPresser,
I would like to add to Javier’s @arques post above. His method does work but it’s missing the initial sign-up screen where gravatar is being pulled. I also rewrote his path a bit. This should do the trick for killing all Gravatar calls. If someone has more time than I it would be great to have this in plugin form. Here is the code:
`
function bp_remove_gravatar ($image, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir) {$default = get_stylesheet_directory_uri() .’/_inc/images/bp_default_avatar.jpg’;
if( $image && strpos( $image, “gravatar.com” ) ){
return ‘‘;
} else {
return $image;}
}
add_filter(‘bp_core_fetch_avatar’, ‘bp_remove_gravatar’, 1, 9 );function remove_gravatar ($avatar, $id_or_email, $size, $default, $alt) {
$default = get_stylesheet_directory_uri() .’/_inc/images/bp_default_avatar.jpg’;
return ““;
}add_filter(‘get_avatar’, ‘remove_gravatar’, 1, 5);
function bp_remove_signup_gravatar ($image) {
$default = get_stylesheet_directory_uri() .’/_inc/images/bp_default_avatar.jpg’;
if( $image && strpos( $image, “gravatar.com” ) ){
return ‘‘;
} else {
return $image;
}}
add_filter(‘bp_get_signup_avatar’, ‘bp_remove_signup_gravatar’, 1, 1 );
`The image that I am referencing doesn’t have to be a JPG. The size is 150×150. You may have to adjust the file name and/or path to your liking.
Thanks @arques for the initial code!
Thank you both very much for this code – very helpful.
And it is disturbing that there is no kill-switch for gravatars in the browser admin.
Thank you everybody for the suggestions. I’m setting up a site for an artist community and I don’t want BuddyPress to assign (IMHO not very attractive) monster gravatars to the members at all. Perhaps a future version could be written to respect the WordPress setting for No Avatar.
You can easily change the default avatar in your dashboard under BuddyPress > General Settings. The reason it doesn’t use the WordPress avatar setting is because BuddyPress is designed to be it’s own site with it’s own settings. If you want to disable gravatar support completely you can set no_grav to true in your theme’s bp_core_fetch_avatar function calls. This will be made even easier when 1.3 is released.
If anyone fancies a real challenge, what I should like to do is make a conditional that says:
If anyone does not have their own avatar, and if they do not have a gravatar, use this image.
Reason being, the result of the standard preference for gravatar if the user has none is the hideously dominant blue gravatar logo. Perhaps I should just replace that default graphic somehow, unless it too is called from gravatar.If anyone fancies a real challenge, what I should like to do is make a conditional that says:
If anyone does not have their own avatar, and if they do not have a gravatar, use this image.
Reason being, the result of the standard preference for gravatar if the user has none is the hideously dominant blue gravatar logo. Perhaps I should just replace that default graphic somehow, unless it too is called from gravatar.Add the following in wp-content/plugins/bp-custom.php:
define( 'BP_AVATAR_DEFAULT', 'http://example.com/my-new-default-avatar.jpg' );
Change the link of the URL to your default avatar URL.
Add the following in wp-content/plugins/bp-custom.php:
define( 'BP_AVATAR_DEFAULT', 'http://example.com/my-new-default-avatar.jpg' );
Change the link of the URL to your default avatar URL.
add_filter('bp_core_fetch_avatar_no_grav', '__return_true');
- The topic ‘How to disable Gravatar completely?’ is closed to new replies.