Created
May 9, 2013 13:31
php - wordpress - custom default Gravatar + localhost support hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* AVATAR */ | |
// Add a default avatar to Settings > Discussion | |
add_filter( 'avatar_defaults', 'add_custom_gravatar' ); | |
if ( !function_exists('add_custom_gravatar') ) { | |
function add_custom_gravatar( $avatar_defaults ) { | |
$myavatar = get_stylesheet_directory_uri() . '/_inc/img/avatar.png'; | |
$avatar_defaults[$myavatar] = 'Avatar Santé Ensemble'; | |
return $avatar_defaults; | |
} | |
} | |
//Hack the default beahvior of gravatar to enable custom avatars on localhost | |
add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 ); | |
function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt ) | |
{ | |
$whitelist = array( 'localhost', '127.0.0.1' ); | |
if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) ) | |
return $avatar; | |
$doc = new DOMDocument; | |
$doc->loadHTML( $avatar ); | |
$imgs = $doc->getElementsByTagName('img'); | |
if ( $imgs->length > 0 ) | |
{ | |
$url = urldecode( $imgs->item(0)->getAttribute('src') ); | |
$url2 = explode( 'd=', $url ); | |
$url3 = explode( '&', $url2[1] ); | |
$avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='42' width='42' />"; | |
} | |
return $avatar; | |
} | |
/* AVATAR */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment