Skip to content

Instantly share code, notes, and snippets.

@dsgnr
Created August 11, 2016 17:13
Show Gist options
  • Save dsgnr/bae007c5777758e9bf2423262cde20f7 to your computer and use it in GitHub Desktop.
Save dsgnr/bae007c5777758e9bf2423262cde20f7 to your computer and use it in GitHub Desktop.
<?php
//* Custom Gravatar
function be_custom_avatar_field( $user ) { ?>
<h3>Custom Avatar</h3>
<table>
<tr>
<th><label for="be_custom_avatar">Custom Avatar URL:</label></th>
<td>
<input type="text" name="be_custom_avatar" id="be_custom_avatar" value="<?php echo esc_attr( get_the_author_meta( 'be_custom_avatar', $user->ID ) ); ?>" /><br />
<span>Type in the URL of the image you'd like to use as your avatar. This will override your default Gravatar, or show up if you don't have a Gravatar. <br /><strong>Image should be 70x70 pixels.</strong></span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'be_custom_avatar_field' );
add_action( 'edit_user_profile', 'be_custom_avatar_field' );
/**
* Save Custom Avatar Field
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-custom-avatar/
*
* @param int $user_id
*/
function be_save_custom_avatar_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_usermeta( $user_id, 'be_custom_avatar', $_POST['be_custom_avatar'] );
}
add_action( 'personal_options_update', 'be_save_custom_avatar_field' );
add_action( 'edit_user_profile_update', 'be_save_custom_avatar_field' );
function be_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) {
// If provided an email and it doesn't exist as WP user, return avatar since there can't be a custom avatar
$email = is_object( $id_or_email ) ? $id_or_email->comment_author_email : $id_or_email;
if( is_email( $email ) && ! email_exists( $email ) )
return $avatar;
$custom_avatar = get_the_author_meta('be_custom_avatar');
if ($custom_avatar)
$return = '<img src="'.$custom_avatar.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" class="avatar" />';
elseif ($avatar)
$return = $avatar;
else
$return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" class="avatar" />';
return $return;
}
add_filter('get_avatar', 'be_gravatar_filter', 10, 5);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment