I need to write a code to tell the browser to preload only my featured images in wordpress and woocommerce
Budget: $10 – $30 USD
The code I write was add_action('wp_head', 'preload_featured_image', 0);
function preload_featured_image(){
if ( has_post_thumbnail() ){
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src( $id, 'full' );
$srcset = wp_get_attachment_image_srcset( $id, 'full' );
$sizes = wp_get_attachment_image_sizes( $id, 'full' );
echo '<link rel="preload" as="image" href="'.$src[0].'" imagesrcset="'.$srcset.'" imagesizes="'.$sizes.'"/>';
}
}
But wordpress added another request to preload the image again so the image is loading twice.
function preload_featured_image(){
if ( has_post_thumbnail() ){
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src( $id, 'full' );
$srcset = wp_get_attachment_image_srcset( $id, 'full' );
$sizes = wp_get_attachment_image_sizes( $id, 'full' );
echo '<link rel="preload" as="image" href="'.$src[0].'" imagesrcset="'.$srcset.'" imagesizes="'.$sizes.'"/>';
}
}
But wordpress added another request to preload the image again so the image is loading twice.