webappguides
Create Wordpress Theme From Scratch! Build Your Own

How to get user profile image URL in WordPress

webapps log

webapps Lab
June 7, 2021
ThemeCreation

In this post, you will find a step-by-step process that will retrieve the user profile image URL in WordPress.

The common way to get the image uses the get_avatar function.

The function itself returns an HTML IMG tag which makes it hard to customize.

But having a profile picture link enables to set the user’s image as a background picture.

And it is simple to customize the image.

In fact, you will be able to set the image width.

WordPress function for avatar URL.

It is the get_avatar_url function that retrieves the author image URL without HTML image tags.

get_avatar_url takes the user id as an argument to return a required avatar link of the author.

The WordPress framework provides the author id using get_the_author_meta.

The code snippet

$theAuthorId = get_the_author_meta('ID');

$getUserUrl = get_avatar_url ($theAuthorId);

A function can set the image size.

The size value is defined in the array by an element with a key of ‘size’.

The array forms a second argument in the get_avatar_url function.

$getUserUrl = get_avatar_url($theAuthorId, array ('size' => 500));

Set up user profile image as a background picture

<div style = “background-image : url (‘<?php echo get_avatar_url($theAuthorId, array (‘size’ => 500));?>’)  ;”>