WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i want to get the user avatar URL to use it as background URL style for a div. i tried to use the following but it does not return anything when i view the code it appears like that.

background: url()

i used this function.

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

any help please??

share|improve this question
Where is the $get_avatar variable coming from? – Joseph Leedy Aug 27 '12 at 12:42

It's fairly simple to construct the Gravatar URL yourself, it's just an MD5 hash of the user's email address.

<?php $gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower($email)) . '&s=32'; ?>

<div class="avatar" style="background: url(<?php echo $gravatar ?>);" ></div>

The s parameter at the end there defines the size of the image in pixels.

Using Gravatars - WordPress Codex

share|improve this answer

function get_avatar_url( $avatar, $id_or_email, $size = 96, $default = '', $alt = '' ) {

    preg_match( '#src=["|\'](.+)["|\']#Uuis', $avatar, $matches );

    return ( isset( $matches[1] ) && ! empty( $matches[1]) ) ?
        (string) $matches[1] : '';  


Apply a filter on get_avatar, it takes 5 arguments (!). The first one is the complete <img>-tag. And do not forget the source can be enclosed with double and single quotes ( ["|'] ). I guess that was the point where your function failed.

share|improve this answer

Your Answer


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.