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??
It’s fairly simple to construct the Gravatar URL yourself, it’s just an MD5 hash of the user’s email address.
The
s
parameter at the end there defines the size of the image in pixels.Using Gravatars – WordPress Codex
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.As of WordPress version 4.2.0, we can use get_avatar_url(). The function can be found in
wp-includes/link-template.php
:So you can simply use it by:
Also, I know this is an old question, but a great lesson to be learned. Always use prefixes when you add your custom functions. Your custom function is
get_avatar_url()
and so is the newly added core functionget_avatar_url()
. You would have received an error when you upgraded to 4.2.0.