In a custom shortcode function, I’m grabbing the featured image URL:
$text_slider_testimonial_img = get_the_post_thumbnail_url($single->ID);
If I echo $text_slider_testimonial_img immediately I see the correct image URL: //localhost:3000/wp-content/uploads/2021/03/splitbanner1.jpg
When I pass this variable to a function, and that function uses:
$text_slider_content .= "<div class='text_slider_testimonial' style='background-size: cover; background-image: url('". $text_slider_testimonial_img ."')>";
return $text_slider_content;
the style component of the above is output as:
style="background-size: cover; background-image: url(" localhost:3000="" wp-content="" uploads="" 2021="" 03="" splitbanner1.jpg')="">
Why are the slashes being stripped out, and the ="" being added?
Help appreciated.
Here is the wider code context (pastebin.com).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Replacing the line with below line may solve the issue.
$text_slider_content .= '<div class="text_slider_testimonial" style="background-size: cover; background-image: url('. $text_slider_testimonial_img .');">';
Method 2
Replacing the line with following should solve the issue:
$text_slider_content .= "<div class='text_slider_testimonial' style='background-size: cover; background-image: url($text_slider_testimonial_img);>";
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0