• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

17 June - 2015 By Paal Joachim 3 Comments
Last updated on: June 17, 2015

How to add your own avatar in WordPress.

Today the default is to sign up at gravatar.com and to add your avatar there. It will then be available in a number of sites including WordPress sites. I am showing a few options if you choose not to use a gravatar but want a local avatar instead.

Adding an avatar without a plugin

Adding an avatar to the Settings -> Discussion area. In the list of avatars.
The code one can use which is to be placed into the Child Theme functions.php file.

 add_filter( 'avatar_defaults', 'custom_avatar' );
    function custom_avatar($avatar_defaults){
    $custom_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.jpg';
    $avatar_defaults[$custom_avatar] = "My Default Avatar";
    return $avatar_defaults;
 }

One can always just change the $custom_avatar = ‘to the full url of the image being used’ instead of get stylesheet directory uri code.

Here I added my shaman picture as a new default avatar to the default avatar list.

Adding Avatar Discussion screen

 

 

By copying the following code by Bill Ericksons add an URL field to the profile screen.

/**************************************************
* Custom avatar link added to the profile page.
* https://www.billerickson.net/wordpress-custom-avatar/
*
**************************************************
/**
 * Add Custom Avatar Field
 * @author Bill Erickson
 * @link https://www.billerickson.net/wordpress-custom-avatar/
 *
 * @param object $user
 */
function be_custom_avatar_field( $user ) { ?>
 <h3>Custom Avatar</h3>
 <table>
 <tr>
 <th><label for="be_custom_avatar"></label></th>
 <td>
 <input type="text" style="width: 100%;" name="be_custom_avatar" id="be_custom_avatar" value="<?php echo esc_attr( get_the_author_meta( 'be_custom_avatar', $user->ID ) ); ?>" />
 <span> <i>Type in the <b>URL</b> of the image you'd like to use as your avatar. <br /><strong>Image should be 70x70 pixels.</strong> <p>This will override your default Gravatar, or show up if you don't have a Gravatar. <br /></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 https://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) {
 $custom_avatar = get_the_author_meta('be_custom_avatar');
 if ($custom_avatar) 
 $return = '<img src="'.$custom_avatar.'" width="'.$size.'" height="'.$size.'" alt="avatar" class="avatar" />';
 elseif ($avatar) 
 $return = $avatar;
 else 
 $return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
 
 return $return;
}
add_filter('get_avatar', 'be_gravatar_filter', 10, 5);

 

I am looking into extending this article when I find additional code…

Here are two avatar plugins

Avatar Manager
WP user Avatar

 

There is a trac ticket for adding a local avatar to WordPress. As it has been asked for for a long time.
https://core.trac.wordpress.org/ticket/16020

Resources:
https://www.wpstuffs.com/how-to-display-custom-avatar-in-comments-for-users-without-gravatar/
https://www.rswebsols.com/tutorials/wordpress/custom-default-avatar-in-wordpress
https://www.wpcodesnippet.com/comments/add-image-default-avatar-list-wordpress/

A tutorial on adding author picture in your biography post.
https://www.gabediaz.com/blog/displaying-author-pic-and-bio-in-your-wordpress-post/

Adding an image uploader tutorial:
https://s2webpress.com/add-image-uploader-to-profile-admin-page-wordpress/

Add an url field to the user profile section.
https://www.billerickson.net/wordpress-custom-avatar/

Share this:

  • Email

Categories: Developer, How to, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. filmr says

    26 November - 2020

    how i can set the avatar in comment list ?

    Reply
  2. Kai says

    31 July - 2019

    Today in most themes this includes the error:

    when comments on a post are made, the commenters all get the avatar-image of the author.

    Reply
  3. dev joshi says

    21 September - 2018

    Really informative content, Keep sharing.

    Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.