Wordpress - Removing Gravatar.com support for WordPress and Simple Local Avatars
If you want a super-lightweight solution and don't mind dabbling in a little code, drop this in your functions.php
;
function __default_local_avatar()
{
// this assumes default_avatar.png is in wp-content/themes/active-theme/images
return get_bloginfo('template_directory') . '/images/default_avatar.png';
}
add_filter( 'pre_option_avatar_default', '__default_local_avatar' );
Alternatively, if you want a bit more juice, with the ability to manage everything in the admin, check out Add New Default Avatar.
UPDATE: I wrote a quick plugin to be used alongside Simple Local Avatars.
<?php
/**
* Plugin Name: Disable Default Avatars
* Plugin URI: http://wordpress.stackexchange.com/questions/17413
* Description: To be used alongside <a href="http://www.get10up.com/plugins/simple-local-avatars-wordpress/">Simple Local Avatars</a>, disabling all default avatars and falling back to a single image. Use the filter <code>local_default_avatar</code> to set the path of the image.
* Version: 1.0
* Author: TheDeadMedic
* Author URI: http://wordpress.stackexchange.com/users/1685/thedeadmedic
*/
if ( !function_exists( 'get_avatar' ) ) :
/**
* Retrieve the avatar for a user who provided a user ID or email address.
*
* @since 2.5
* @param int|string|object $id_or_email A user ID, email address, or comment object
* @param int $size Size of the avatar image
* @param string $default URL to a default image to use if no avatar is available
* @param string $alt Alternate text to use in image tag. Defaults to blank
* @return string <img> tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
if ( ! get_option('show_avatars') )
return false;
static $default_url; // use static vars for a little caching
if ( !isset( $default_url ) )
$default_url = apply_filters( 'local_default_avatar', get_template_directory_uri() . '/images/default_avatar.png' );
if ( false === $alt)
$safe_alt = '';
else
$safe_alt = esc_attr( $alt );
if ( !is_numeric( $size ) )
$size = '96';
$avatar = "<img alt='{$safe_alt}' src='{$default_url}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
}
endif;
function __limit_default_avatars_setting( $default )
{
return 'local_default';
}
add_filter( 'pre_option_avatar_default', '__limit_default_avatars_setting' );
if ( is_admin() ) :
function __limit_default_avatars( $defaults )
{
return array( 'local_default' => get_bloginfo( 'name' ) . ' Default' );
}
add_filter( 'avatar_defaults', '__limit_default_avatars' );
endif;
?>
The plugin description should be self-explanatory, but essentially the default avatar is http://wordpress-theme-path/images/default_avatar.png
, which can be filtered in your theme's functions
.php if it exists elsewhere;
function __my_theme_default_avatar( $url )
{
return 'http://somewhere/else.jpg';
}
add_filter( 'local_default_avatar', '__my_theme_default_avatar' );
I successfully replaced all gravitar.com avatars with my own locally hosted avatar image by adding this to my theme's function.php
file:
define('DEFAULT_AVATAR_URL', get_template_directory_uri() . '/images/user.png');
function no_gravatars( $avatar ) {
return preg_replace( "/http.*?gravatar\.com[^\']*/", DEFAULT_AVATAR_URL, $avatar );
}
add_filter( 'get_avatar', 'no_gravatars' );
This assumes you have a local default avatar called user.png
in your theme's images
subdirectory.
In the discussion settings
admin panel, make sure "Show Avatars" is ticked.
I haven't tested this with Simple Local Avatars, but it works great with Basic User Avatars (which is a lightweight fork of Simple Local Avatars).
TIP: If you're designing your own default avatar, I recommend making it 300x300 pixels so it will look good on "retina" screens when WP displays it at "150x150". Then you can significantly reduce the filesize of the image by compressing it at tinyPNG.com.