Wrong URL is returned when preloading featured image
-
Hi there! I’m trying to optimize our website by preloading the featured image but it appears to be returning the JPG URL when it’s the WEBP that is displayed. When the page is loaded, I see this warning in the browser’s console log:
The resource
{CDN URL REDACTED}/GettyImages-1061745418-1142x493.jpgwas preloaded using link preload but not used within a few seconds from the window’s load event. Please make sure it has an appropriateasvalue and it is preloaded intentionally.When I go to inspect the file that is visible on the page, I see the extension as
GettyImages-1061745418-1142x493.jpg.webpin the HTML for the image element.I’m using WordPress’s native functions for retrieving the URL, specifically:
global $post; if(has_post_thumbnail($post->ID)) { $image_size = 'hero'; $featured_image_id = get_post_thumbnail_id($post->ID); $featured_image_url = wp_get_attachment_image_src($featured_image_id, $image_size); echo sprintf( '<link id="preload-featured-image" rel="preload" as="image" href="%s" data-size="%s" data-id="%s" />', $featured_image_url, $image_size, $featured_image_id ); }And then when it’s displayed on the frontend, it also uses WordPress functions:
global $post; if(has_post_thumbnail($post->ID)) { $featured_image_id = get_post_thumbnail_id($post->ID); echo wp_get_attachment_image($featured_image_id, 'hero', false, array( 'loading' => false, 'alt' => get_post_meta($featured_image_id, '_wp_attachment_image_alt', true) )); }I guess what I’m saying is that I’m experiencing inconsistencies with
wp_get_attachment_image_src()andwp_get_attachment_image(). When I get the HTML for the whole image tag in the latter, it correctly loads the WEBP format, but when I use the former to get just the URL, it only gives me the original JPG format.In my settings, I’ve got the box checked for WebP Conversion.
Under WebP Delivery Method, I’ve got the box checked for JS WebP Rewriting since we’re using a CDN (seemed like the right choice, maybe this is the issue?)
And then I’ve got my uploads and active theme directory with the CDN hostname set in WebP URLs.
Any help is appreciated. Thanks!
The topic ‘Wrong URL is returned when preloading featured image’ is closed to new replies.