Create wordpress function to get post author gravatar url as template tag

I need a code implemented within function.php so I can get post author gravatar url as template tag get_author_gravatar_url().

So far I am using template tag my_gravatar_url() using function:

function my_gravatar_url() { // Get user email
$user_email = get_the_author_meta( 'user_email' );
/* Convert email into md5 hash and set image size to 65 px */
$user_gravatar_url = 'http://www.gravatar.com/avatar/' . md5($user_email) . '?s=150';
echo $user_gravatar_url; }

Related posts

1 comment

  1. You just need to modify the function slightly to return the value (rather than displaying it):

    function get_author_gravatar_url() { 
        // Get user email
        $user_email = get_the_author_meta( 'user_email' );
        // Convert email into md5 hash and set image size to 65 px
        $user_gravatar_url = 'http://www.gravatar.com/avatar/' . md5($user_email) . '?s=150';
    
        return $user_gravatar_url; 
    }
    

Comments are closed.