Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a plugin called Simple Local Avatars which lets me upload author images which are stored on my server locally (no Gravatar). The plugin works fine and get_avatar returns the local avatar.

However, I need to use that avatar in different ways and different places and for that I need the local avatar image URL instead of the entire HTML tag. I could write a wrapper function for get_avatar that uses RegEx or SimpleXML to pick out and return only the URL, but I was wondering if there's any existing way to do this.

share|improve this question

3 Answers 3

The answer above seems comprehensive, but I just wrote a wrapper function and moved on. Here it is if you need it (put this in functions.php):

function get_avatar_url($get_avatar){
    preg_match("/src='(.*?)'/i", $get_avatar, $matches);
    return $matches[1];
}

and then use it wherver you need it in the template files like this:

<img src="<? echo get_avatar_url(get_avatar( $curauth->ID, 150 )); ?>" align="left" class="authorimage" />

It's just simpler.

Using RegEx to parse HTML in this case is okay, because this will only be parsing one img tag, so it won't be too costly.

share|improve this answer
    
A small change... the get_avatar function puts the src within " not ' so the match will be null. The regex should be preg_match('/src="(.*?)"/i', $get_avatar, $matches); –  spdaly Sep 17 at 12:43

You can use the filter get_avatar for get all data to the avatar, also the url inside the markup. I think, WP don`t have an function for return only the url if the avatar image.

$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";

apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);

Also you can rewrite this function inside an plugin or theme, the function is onyl active, if this function name is not on other place defined.

if ( ! function_exists( 'get_avatar' ) ) :

So it is possible to add an param for return only the url of the image, like this, use the param $url with TRUE and you get only the url.

/**
 * Retrieve the avatar for a user who provided a user ID or email address.
 *
 * @since 2.5
 * @param int|string|object $id_or_email A user ID,  email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternate text to use in image tag. Defaults to blank
 * @param boolean $url, true for get only the url of the image, no markup
 * @return string <img> tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false, $url = FALSE ) {
    if ( ! get_option('show_avatars') )
        return false;

    if ( false === $alt)
        $safe_alt = '';
    else
        $safe_alt = esc_attr( $alt );

    if ( !is_numeric($size) )
        $size = '96';

    $email = '';
    if ( is_numeric($id_or_email) ) {
        $id = (int) $id_or_email;
        $user = get_userdata($id);
        if ( $user )
            $email = $user->user_email;
    } elseif ( is_object($id_or_email) ) {
        // No avatar for pingbacks or trackbacks
        $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
        if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
            return false;

        if ( !empty($id_or_email->user_id) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_userdata($id);
            if ( $user)
                $email = $user->user_email;
        } elseif ( !empty($id_or_email->comment_author_email) ) {
            $email = $id_or_email->comment_author_email;
        }
    } else {
        $email = $id_or_email;
    }

    if ( empty($default) ) {
        $avatar_default = get_option('avatar_default');
        if ( empty($avatar_default) )
            $default = 'mystery';
        else
            $default = $avatar_default;
    }

    if ( !empty($email) )
        $email_hash = md5( strtolower( trim( $email ) ) );

    if ( is_ssl() ) {
        $host = 'https://secure.gravatar.com';
    } else {
        if ( !empty($email) )
            $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
        else
            $host = 'http://0.gravatar.com';
    }

    if ( 'mystery' == $default )
        $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
    elseif ( 'blank' == $default )
        $default = includes_url('images/blank.gif');
    elseif ( !empty($email) && 'gravatar_default' == $default )
        $default = '';
    elseif ( 'gravatar_default' == $default )
        $default = "$host/avatar/?s={$size}";
    elseif ( empty($email) )
        $default = "$host/avatar/?d=$default&amp;s={$size}";
    elseif ( strpos($default, 'http://') === 0 )
        $default = add_query_arg( 's', $size, $default );

    if ( !empty($email) ) {
        $out = "$host/avatar/";
        $out .= $email_hash;
        $out .= '?s='.$size;
        $out .= '&amp;d=' . urlencode( $default );

        $rating = get_option('avatar_rating');
        if ( !empty( $rating ) )
            $out .= "&amp;r={$rating}";

        if ( $url )
            $avatar = $out;
        else
            $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    } else {
        if ( $url )
            $avatar = $out;
        else
            $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
    }

    return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}

Another small variant is, that you create the url with the rule of Gravatar.

function get_gravatar_url( $email ) {

    $hash = md5( strtolower( trim ( $email ) ) );
    return 'http://gravatar.com/avatar/' . $hash;
}

use this on your source with the emails of authors and you get the url of there image.

share|improve this answer

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, $matches);
    return ( $matches[1] );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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