Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: How to disable Gravatar completely?


Marcus (aka @msykes)
Participant

@netweblogic

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.

Skip to toolbar