Created
September 28, 2023 16:00
-
-
Save PropelDMS/65672498ab6403d43c5369ae77e12ab0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Preload Featured Images | |
function propel_add_cwv_code(){ | |
global $post; | |
// Run code only for Single post page | |
if( is_admin() ){ | |
return; | |
} | |
$post_id = $post->ID; | |
$image_id = get_post_thumbnail_id( $post_id ); | |
$featured_image_url_mobile = wp_get_attachment_image_src( $image_id, 'image-480' )[0]; | |
if ( $featured_image_url_mobile && strlen( $featured_image_url_mobile ) > 0 ) { | |
//Comment line below out if you do not have webp versions of your images | |
$featured_image_url_mobile = str_replace( '.jpg', '.webp', $featured_image_url_mobile); | |
?> | |
<link rel="preload" href="<?php echo $featured_image_url_mobile; ?>" as="image" media="(max-width: 480px)"> | |
<?php | |
} | |
} | |
add_action('wp_head','propel_add_cwv_code', 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment