Get URL of logged in user avatar

Hello,

Can anyone help me work out how to return the url of the currently logged in user's avatar?

I know there are various plugins and shortcodes out there to return the user's avatar, but none (that i could find) will return just the url on its own (they tend to return something like <img src="the bit i want" width="x" height="y" /img> )

Does anyone have a code snipped for this?

Thanks :slight_smile:

  • Matt
    • Design Lord, Child of Thor

    Hi Kasia,

    Thanks very much for your reply. Unfortunately this doesn't quite work in my case as it returns the gravatar URL. However, i am using a membership plugin that uses local avatars (so the URL will also be local).

    function wpmudev_return_grav_url() {
    if ( is_user_logged_in() ) {
    	global $current_user;
    	get_currentuserinfo();
    	return get_avatar_url( $current_user->ID, $size = '50' );
    	}
    }
    add_shortcode('logged-in-user-avatar-url', 'wpmudev_return_grav_url');

    This returns a gravatar url as mentioned above. However, the code below does return the correct (local) user avatar. Is there some way to extract just the url from this? (at the moment it returns
    <img src="the bit i want" width="270" height="270" alt="" class="modified avatar"> )

    function logged_in_user_avatar_shortcode() {
    	if ( is_user_logged_in() ) {
    		global $current_user;
    		get_currentuserinfo();
    		return get_avatar( $current_user->ID, 270 );
    	}
    }
    add_shortcode('logged-in-user-avatar', 'logged_in_user_avatar_shortcode');

    I hope that makes sense :slight_smile:

    Thanks!

  • Ash
    • WordPress Hacker

    Hello Matt

    Please try the following code:

    function logged_in_user_avatar_shortcode() {
    	if ( is_user_logged_in() ) {
    		global $current_user;
    		get_currentuserinfo();
    		$avatar = get_avatar( $current_user->ID, 270 );
                    $avatar_arr = explode( '"', $avatar );
                    return $avatar_arr[1];
    	}
    }
    add_shortcode('logged-in-user-avatar', 'logged_in_user_avatar_shortcode');

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Matt
    • Design Lord, Child of Thor

    Hi again,

    I obviously managed to get this to work before, but now i cant figure out for the life of me how to do it!

    I want to return the URL of the current user avatar. I want to call it in my header.php file

    I have added this to functions.php:

    function logged_in_user_avatar_shortcode() {
    	if ( is_user_logged_in() ) {
    		global $current_user;
    		get_currentuserinfo();
    		$avatar = get_avatar( $current_user->ID, 270 );
                    $avatar_arr = explode( '"', $avatar );
                    return $avatar_arr[1];
    	}
    }
    add_shortcode('logged-in-user-avatar', 'logged_in_user_avatar_shortcode');

    And this to header.php

    <?php echo do_shortcode( 'logged-in-user-avatar' ); ?>

    But it is simply returning 'logged-in-user-avatar' as text, rather than the URL, so i've obviously done something wrong somewhere! Can anybody help me out?

    Thanks

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.