0

I've made a small function which checks if a user has an avatar. If so, the avatar is set as a background URL for a div. However, when checking the console on the frontend, slashes are missing from the URL, thus not displaying the background image.

function small_avatar($user_ID){
$avatar = get_user_meta($user_ID, 'avatar_user', true);
if(!empty($avatar)){
 return "<div class='setAvatar clan_avatar' style='background: url('".$avatar."');'></div>";
 }
}

The use of " and ' is probably breaking something but.. I can't figure it out. Tried to set the style='background' as a variable, and tried to return that as well. No luck.

asked Apr 20, 2017 at 13:33
1
  • the syntax seems ok. Are you sure $avatar has the string with shlashes? Commented Apr 20, 2017 at 13:37

1 Answer 1

0

Sigh..

All I needed to do was remove the " from the url()

So this works:

function small_avatar($user_ID){
$avatar = get_user_meta($user_ID, 'avatar_user', true);
if(!empty($avatar)){
return "<div class='setAvatar clan_avatar' style='background: url(".$avatar.");'></div>";
}
}
answered Apr 20, 2017 at 13:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.