How to get Avatar Image URL in wordpress

How to get Avatar Image URL in wordpress

It’s easy to use avatar in wordpress withe the function get_avatar (See codex here). The default usage is as:

<?php echo get_avatar( $id_or_email, $size, $default, $alt ); ?>

And it will display an img HTML element like:

<img src="" alt="" class="" height="" width="" />

If you want to alter the elements returned by get_avatar, you can add a filter to it (See filter reference here).

But what if you want to get the Avatar image URL itself so you can better use it in your own way?

Put following snippet in your functions.php.

/*
 * Get Gravatar image URL
 * URL: http://wpsites.org/?p=10444
 */
function get_avatar_url( $id_or_email, $args = null ) {
	$original_args = $args;
	$args = wp_parse_args( $args, array(
		'size'           => 90,
		'default'        => get_option( 'avatar_default', 'mystery' ),
		'force_default'  => false,
		'rating'         => get_option( 'avatar_rating' ),
		'scheme'         => null,
		'processed_args' => null, // if used, should be a reference
	) );
	if ( is_numeric( $args['size'] ) ) {
		$args['size'] = absint( $args['size'] );
		if ( !$args['size'] ) {
			$args['size'] = 90;
		}
	} else {
		$args['size'] = 90;
	}
	if ( empty( $args['default'] ) ) {
		$args['default'] = 'mystery';
	}
	switch ( $args['default'] ) {
	case 'mm' :
	case 'mystery' :
	case 'mysteryman' :
		$args['default'] = 'mm';
		break;
	case 'gravatar_default' :
		$args['default'] = false;
		break;
	}
	$args['force_default'] = (bool) $args['force_default'];
	$args['rating'] = strtolower( $args['rating'] );
	$args['found_avatar'] = false;
	$url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) );
	if ( !is_null( $url ) ) {
		$return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) );
		$args['processed_args'] = $args;
		unset( $args['processed_args']['processed_args'] );
		return $return;
	}
	$email_hash = '';
	$user = $email = false;
	if ( is_numeric( $id_or_email ) ) {
		$user = get_user_by( 'id', absint( $id_or_email ) );
	} elseif ( is_string( $id_or_email ) ) {
		if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
			// md5 hash
		        list( $email_hash ) = explode( '@', $id_or_email );
		} else {
			// email address
			$email = $id_or_email;
		}
	} elseif ( is_object( $id_or_email ) ) {
		if ( isset( $id_or_email->comment_ID ) ) {
			// Comment Object
			// 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 ) ) {
				$args['processed_args'] = $args;
				unset( $args['processed_args']['processed_args'] );
				return false;
			}
			if ( ! empty( $id_or_email->user_id ) ) {
				$user = get_user_by( 'id', (int) $id_or_email->user_id );
			}
			if ( ( !$user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
				$email = $id_or_email->comment_author_email;
			}
		} elseif ( ! empty( $id_or_email->user_login ) ) {
			// User Object
			$user = $id_or_email;
		} elseif ( ! empty( $id_or_email->post_author ) ) {
			// Post Object
			$user = get_user_by( 'id', (int) $id_or_email->post_author );
		}
	}

	if ( !$email_hash ) {
		if ( $user ) {
			$email = $user->user_email;
		}
		if ( $email ) {
			$email_hash = md5( strtolower( trim( $email ) ) );
		}
	}
	if ( $email_hash ) {
		$args['found_avatar'] = true;
	}
	$url_args = array(
		's' => $args['size'],
		'd' => $args['default'],
		'f' => $args['force_default'] ? 'y' : false,
		'r' => $args['rating'],
	);
	$url = sprintf( 'http://%d.gravatar.com/avatar/%s', hexdec( $email_hash[0] ) % 3, $email_hash );
	$url = add_query_arg(
		rawurlencode_deep( array_filter( $url_args ) ),
		set_url_scheme( $url, $args['scheme'] )
	);
	$return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) );
	$args['processed_args'] = $args;
	unset( $args['processed_args']['processed_args'] );
	return $return;
}

Usage:

The above function will return an image URL for giver user ID/Email, so if you want to output the URL, you have to echo the return value.

<?php echo get_avatar_url( $id_or_email );?>

Thank you pathawks for this great snippet and you can find detailed documentation here.

[box type=”note”]UPDATE: This function was already included in WordPress core functions since 4.2. Codex: https://developer.wordpress.org/reference/functions/get_avatar_url/[/box]
Save

Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

1 thought on “How to get Avatar Image URL in wordpress”

Leave a Reply

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