Image field URLs are missing slashes. I am using an ACF image field, and I’ve tried returning both an array and an ID, and in both cases, the URL of the image file is missing the slashes. So the URL will look like:
"http:// mysite.com wp-content uploads 2018 11 image.jpg"
There are spaces where the slashes should be. I am using the most recent version of ACF Pro and WP, and I have no other plugins installed. I have confirmed that image URLs are correct if I add them into the main WYSIWYG editor or as featured image. I use ACF images all the time, and have never seen this issue. I’m on WP Engine hosting if that matters.
Here’s how I’m getting the URLs:
(array)
$bg = get_sub_field('section_background_image');
$bgurl = $bg['sizes']['large'];
echo $bgurl;
(ID)
$bgurl = wp_get_attachment_image_src( $bg, 'large' );
echo $bgurl[0];
Any ideas what might be happening here?
I also just tried returning the field as simply the URL (not an array or ID) and the issue exists there as well.
Ok, I don’t know how to explain it, but I guess my field got corrupted? I created a new image field with same settings, deleted the old field, and it worked. False alarm!
In case anybody else finds this, I had the same issue when displaying an image as a background image. I was using something like:
<?php if ($photo) { echo 'background-image: url("' . esc_url($photo['url']) . '");'; } ?>">
Turns out the double quotes within the url(“”) caused the file name to output without slashes. I believe that’s valid CSS but I guess PHP doesn’t like it. Solved it by removing the double quotes:
<?php if ($photo) { echo 'background-image: url(' . esc_url($photo['url']) . ');'; } ?>">
Or by using single (escaped) quotes:
<?php if ($photo) { echo 'background-image: url(\'' . esc_url($photo['url']) . '\');'; } ?>">
The topic ‘No slashes in image URLs’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.