Hot answers tagged

26

Good news for WordPress versions 4.2+ Since version 4.2 the handy get_avatar_url() function, introduced as a feature request in ticket #21195 few years ago, now ships with the core: /** * Retrieve the avatar URL. * * @since 4.2.0 * * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * ...


8

Putting the following inside loop should fulfill your needs: <?php $get_author_id = get_the_author_meta('ID'); $get_author_gravatar = get_avatar_url($get_author_id, array('size' => 450)); if(has_post_thumbnail()){ the_post_thumbnail(); } else { echo '<img src="'.$get_author_gravatar.'" alt="'.get_the_title().'" />'; } ?>


7

As an alternative to my other answer, you can also use the get_avatar filter. Props to Sumit to alerting me to this one. The benefit of using the get_avatar filter is your custom avatar should be applied anywhere Wordpress uses it, rather than just in this users list like my other answer deals with. If you use plugins that display user avatars, this ...


6

The avatar_default is the better option. add_filter('pre_option_avatar_default', 'se72578_avatar_default'); function se72578_avatar_default( $option ){ return 'http://example.com/your/default/here.jpg'; } Simple. Stick that in a mu-plugin and you're good to go. Edit: If you want to still allow site owners to change the default avatar, use the ...


6

Hey pass the current user email id in get_avatar() function if user is logged in like this <?php if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); if ( ($current_user instanceof WP_User) ) { echo 'Welcome : ' . esc_html( $current_user->display_name ); echo get_avatar( $current_user->ID, 32 ); } }


5

Must Use Plugins will anable functionality global. And actualy functionality is pretty simple... only prefiltring default_avatar value. Use this code as plugin which you can install to anable default avatar. <?php /* Plugin Name: Network Default Avatar Description: This Plugin Used to be the General Framework Author: Version: ...


5

I am not sure why your query is returning more IDs than necessary. The $args for get_users look correct. By default get_users does not support orderby=rand, but you can overwrite that option. See below: function random_user_query( &$query ) { $query->query_orderby = "ORDER BY RAND()"; } // Usage: [random_users how_many = 3] add_shortcode( '...


4

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( '...


4

I think that i figured it out. Gravatar default images need to be on a public url and not one blocked by htaccess. So i had this on my dev server and when i removed my htaccess file that was blocking access the images started to work. This seems to still be a new feature. Hope that this helps out someone in the future.


4

Notice: get_the_author_ID is deprecated since version 2.8! Use get_the_author_meta('ID') instead. in C:\Users\Administrator\Desktop\www.wpsites.dev\wp-includes\functions.php on line 2908 You aren't using get_the_author_meta('ID'); as instructed by the Notice. You are just using get_the_author_meta(). The latter returns an empty string when I try it. ...


3

You can just update the option field avatar_default to your new image url: add_filter( 'avatar_defaults', 'mytheme_default_avatar' ); function mytheme_default_avatar( $avatar_defaults ) { $avatar = get_option('avatar_default'); $new_avatar_url = get_template_directory_uri() . '/images/default_avatar.png'; if( $avatar != $new_avatar_url ) {...


3

You need a little more code for the avatar_defaults hook to work than the Ray Mitchell answer, I found this code on the wordpress codex pages and it worked fine thanks to the normalized user id passed on to the default avatar hook (this works either with a simple user id or email AND with the object user itself) // Apply filter add_filter( 'get_avatar' ,...


3

The Original Poster was missing the pair priority, parameters when declaring the filter hook: add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 ); Being 5 all the parameters the callback function can use: public function get_avatar( $avatar, $id_or_email, $size, $default, $alt ) { return $avatar; }


3

As @shanebp said: You can define the default avatar site-wide by using BP_AVATAR_DEFAULT For this you can define the constant(s) BP_AVATAR_DEFAULT and/or BP_AVATAR_DEFAULT_THUMB inside the bp-custom.php as described at the buddypress codex page Customizing BuddyPress Avatars, for example like this: Code: $upload_dir = wp_upload_dir(); $...


3

The function you're looking for is get_avatar - you should put in something like this: <?php echo get_avatar( get_the_author_meta( 'ID' )); ?>


3

I had this problem, too. Here's the solution for version 4.7.3 if anyone comes across this. get_avatar( $id_or_email = get_the_author_meta( 'user_email' ), $size = '60', $default, $alt, $args = array( 'class' => array( 'd-block', 'mx-auto' ) ) ); or shorter version get_avatar( get_the_author_meta( 'user_email' ), '60', $default, $alt, array( 'class' =&...


3

OK, so there are some occurrences of get_avatar() in your site. If you'll take a look at docs for this function, you'll see, that: you pass URL for default avatar image as 3rd param, you pass args as 5th param. And one of these args is: force_default (bool) (optional) Whether to always show the default image, never the Gravatar. Default: false. And ...


2

get_user_meta($userId, 'simple_local_avatar'); Simple Local Avatars uses meta fields to store the avatar, so you can simply retrieve the value(s) by calling get_user_meta and grabbing the 'simple_local_avatar' field. You'll get returned an array like so: array ( [full] => 'http://...', [96] => 'http://...', [32] => 'http://...' )


2

I think this a better version of aalaap's answer: // In your template ... $avatar_url = get_avatar_url ( get_the_author_meta('ID'), $size = '50' ); // Get src URL from avatar <img> tag (add to functions.php) function get_avatar_url($author_id, $size){ $get_avatar = get_avatar( $author_id, $size ); preg_match("/src='(.*?)'/i", $get_avatar, $...


2

In my mu-plugins folder I now have the following code, which adds a custom avatar as the default network wide, but allows each individual site to change it thereafter. <?php /* Plugin Name: Network Default Avatar Description: This Plugin sets the default avatar network-wide */ class Network_Default_Avatars { // configure these ...


2

Three simple steps to generate a Facebook Avatar: Create an extra field in your comment form for the user's facebook profile (see e.g. WP Engineer for a code example and how to) If the user filled out this field, get the facebook avatar (see this question) Replace the WP avatar with your facebook avatar if a fb profile url is set All together in this Gist (...


2

Debug ideas: You could try to see if this has any effect: add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' ); But you should check out the parameters that go through the bp_core_fetch_avatar filter to see if they are correct (untested): add_filter( 'bp_core_fetch_avatar', 'my_bp_core_fetch_avatar', 99, 9 ); function my_bp_core_fetch_avatar( $...


2

Your code actually works, it is just that you can't do what you want. The way gravatar works is by serving the image if it has one and redirecting to the supplied default if it doesn't, so even if the default is on your server you still can't avoid the redirect. And gravatar really makes your site load faster makes your server less loaded enables the ...


2

The function get_avatar's 3rd argument is the default image, which you can also pass a function's return value to, so wherever you run get_avatar in your theme you can set a function that changes the default avatar as a 3rd argument. get_avatar( get_the_author_meta( 'user_email' ), 64, 'http://example.com/path/to/image.jpg' ); Or, with a function's return ...


2

When looking at the source of get_avatar(), you can see that it takes either ID or email as 1st argument: get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ); The source of get_the_author_meta() shows that it will return data for the following fields: 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ...


2

Have you tried to specify the $userID parameter of get_the_author_meta(), like shown below: echo get_avatar(get_the_author_meta( 'ID', $post->post_author ), 150); This uses the post_author key from your secondary query, which has the ID as value, that said you should be able to shorten your code to: echo get_avatar( $post->post_author, 150);


2

As far as I know the get_avatar() function only allows for square values. That doesn't mean you couldn't use styling to display a rectangular avatar. Essentially you would use styling to "shave off" 150px from the width. So, let's assume that your theme file produces a 300 x 300 pixel avatar for the post author using the following code: <?php echo ...


2

I also use Simple Local Avatars with Theme my login . I have had the same problem. My solution is very simple .Check form in profile-form.php template directory. And add enctype attribute to form (enctype="multipart/form-data" )


2

We could also use one of the following filters, available since WordPress 4.2: pre_get_avatar_data get_avatar_url get_avatar_data Regarding how to get the user ID from the $id_or_email we can see how it's done in the core: $email_hash = ''; $user = $email = false; if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { $...


2

We can use the get_avatar_url filter to modify the avatar's url. If all the relevant get_avatar() calls, have user id as an input argument, then it's easy to get the corresponding user's meta value, with get_user_meta(), within the filter's callback. Otherwise we need to handle all possible input cases, for get_avatar( $some_input ), as it supports: ...


Only top voted, non community-wiki answers of a minimum length are eligible