2

I need to get the post author profile picture link.

I know I can use get_avatar(); but it displays whole image markup

<img src="http://.../img.png" class="avatar img-responsive" alt="image">

But need only the src of the author profile pic.

How can I got that ? Maybe we can use some php or js to take out the only src value from the total output.

My second question is, how can I add that image as post_thumbnail if no thumbnail exist for that post ?

4

5 Answers 5

9

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().'" />';
}
?>
1

just use the function get_avatart_uri() and pass in the user's email as the parameter it will give you the avatar image url

2
0

Beside using get_avatar_url, you can still use get_avatar. If I'm not wrong, this is what you're looking for.

Question 1

Get the post author profile picture link


<?php 
    // only output if gravatar function exists
    if (function_exists('get_avatar')) { ?>

    <?php
    // Convert email into md5 hash and set image size to 32px
    $gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower($user_email)) . '&s=32';

    // Convert email into md5 hash and remove image size to use as post image
    $gravatar_bg = 'http://www.gravatar.com/avatar/' . md5(strtolower($user_email)) . '';
    ?>
    <!-- Output the gravatar as img src -->
    <img src="<?php echo "$gravatar";?>" alt="">

    <!-- Output the gravatar as background url -->
    <div class="avatar" style="background: url(<?php echo $gravatar_bg ?>);" ></div>

<?php } ?>

Question 2

Add that image as post_thumbnail if no thumbnail exist for that post


<?php
    // Check if post has thumbnail 
    // if ( has_post_thumbnail() ) {            // option 1
    if ( '' != get_the_post_thumbnail() ) {     // option 2

    // if has, output the thumbnail
      the_post_thumbnail();

    } else {
     // if not, show the gravatar image
     echo '<img src="<?php echo "$gravatar";?>" alt="">';
    }
?>

Hope this helps, good luck!


Resources: Using Gravatars | hast_post_thumbnail

2
  • Hi, I know I can check if(get_the_post_thumbnail()) and output that image in template. But that doesn't set the post_thumbnail, it only output on that particular template. I want it to set_post_thumbnail. How to do this ? Commented Sep 6, 2015 at 9:36
  • Based on your comment, I don't quite understand what you're trying to accomplish here. Can you be more specific? You asked how to add that image as post_thumbnail if no thumbnail exist for that post, the code I posted does just that. Commented Sep 6, 2015 at 17:47
0

UPDATE(22 May, 2021) - I am using a plugin Simple Local Avatars to allow users to update images locally. The below solution will only work if you use this plugin.


Just In case someone is looking for full image of author and not finding it. Here is a little hack.

Simple Method

$avatar_url  = get_avatar_url( $author_id ); //this will give 96x96 image.
$final_url   = str_replace('-96x96.', '.',$author_avatar_url);

Replacing the -96x96. with just a ., Just need to make sure Image name doesn't include this string. Alternatively you can look for the same string from reverse and do the replace using substr_replace() like:

More Accurate Method

$avatar_url  = get_avatar_url( $author_id ); //this will give 96x96 image.
$replace   = '-96x96.';
$pos = strpos($avatar_url, $replace, -(strlen($replace)+5)); // +5 for length of ext
$final_url = substr_replace($avatar_url, '.', $pos, strlen($replace));

This is the first result on google while looking for full image of author so it might help someone.

2
  • I don't think that would work for the default Gravatars: the size isn't in the URL like that, it's a ?s= parameter. How do you have your avatars set up? Besides get_avatar_url has a documented 'size' parameter for the args array.
    – Rup
    Commented May 13, 2021 at 10:48
  • @Rup Sorry!!! You are right, I am using "Simple Local Avatars"(wordpress.org/plugins/simple-local-avatars) for allowing users to set their profile images locally and I think the plugin is allowing me to get a URL in the format I am getting. Completely forgot about this plugin. I'll add this plugin info in the answer above. Commented May 22, 2021 at 18:01
0

This is all I do to add the registered user's avatar.

<img src="<?php echo get_avatar_url(get_the_author_meta('ID')); ?>" class="YOUR CSS CLASS">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.