The Largest Contentful Paint (LCP) metric measures the time it takes for the largest image or text block on your page to render above the fold. A low LCP time means that the most important content on your page will appear quickly, which will make your website more user-friendly.

One way to improve your LCP time is to preload the LCP image. This tells the browser to start loading the image as soon as possible, so it will be ready to appear when the user scrolls to the top of the page.

Adding a preload tag to your WordPress theme

You will need to add the following code to your WordPress theme’s functions.php file. You can do this by following these steps:

  1. Go to your WordPress dashboard and click on the “Appearance” tab.
  2. Click on the “Theme Editor” button.
  3. In the file list, locate the functions.php file.
  4. Click on the “Edit” button.
  5. Paste the following code into the functions.php file.
  6. Click on the “Update File” button.

Adding a preload tag to your WordPress theme using cPanel

  1. Go to your cPanel dashboard.
  2. Click on the “File Manager” icon.
  3. In the file list, locate the theme folder that you want to add the preload tag to.
  4. Click on the “Edit” button for the functions.php file.
  5. Paste the following code into the functions.php file:
  6. Click on the “Save” button.
  7. Reload your website.
/**
 * Preload attachment image, defaults to post thumbnail
 */
function preload_post_thumbnail() {
    global $post;
    /** Prevent preloading for specific content types or post types */
    if ( ! is_singular() ) {
        return;
    }
    /** Adjust image size based on post type or other factor. */
    $image_size = 'full';

    if ( is_singular( 'product' ) ) {
        $image_size = 'woocommerce_single';

    } else if ( is_singular( 'post' ) ) {
        $image_size = 'large';

    }
    $image_size = apply_filters( 'preload_post_thumbnail_image_size', $image_size, $post );
    /** Get post thumbnail if an attachment ID isn't specified. */
    $thumbnail_id = apply_filters( 'preload_post_thumbnail_id', get_post_thumbnail_id( $post->ID ), $post );

    /** Get the image */
    $image = wp_get_attachment_image_src( $thumbnail_id, $image_size );
    $src = '';
    $additional_attr_array = array();
    $additional_attr = '';

    if ( $image ) {
        list( $src, $width, $height ) = $image;

        /**
         * The following code which generates the srcset is plucked straight
         * out of wp_get_attachment_image() for consistency as it's important
         * that the output matches otherwise the preloading could become ineffective.
         */
        $image_meta = wp_get_attachment_metadata( $thumbnail_id );

        if ( is_array( $image_meta ) ) {
            $size_array = array( absint( $width ), absint( $height ) );
            $srcset     = wp_calculate_image_srcset( $size_array, $src, $image_meta, $thumbnail_id );
            $sizes      = wp_calculate_image_sizes( $size_array, $src, $image_meta, $thumbnail_id );

            if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
                $additional_attr_array['imagesrcset'] = $srcset;

                if ( empty( $attr['sizes'] ) ) {
                    $additional_attr_array['imagesizes'] = $sizes;
                }
            }
        }

        foreach ( $additional_attr_array as $name => $value ) {
            $additional_attr .= "$name=" . '"' . $value . '" ';
        }

    } else {
        /** Early exit if no image is found. */
        return;
    }

    /** Output the link HTML tag */
    printf( '<link rel="preload" as="image" href="%s" %s/>', esc_url( $src ), $additional_attr );
}
add_action( 'wp_head', 'preload_post_thumbnail' );

Once you have added the code to your functions.php file, you will need to reload your website. The preload tag will then be added to the head of the document, and the post-thumbnail image will start loading as soon as possible.

Buy Me A Coffee

Preloading the LCP image is a great way to improve the Largest Contentful Paint (LCP) time of your WordPress website. By following the steps in this blog post, you can make your website more user-friendly and improve your chances of ranking higher in search engine results pages (SERPs).

In addition to the steps outlined above, there are a few other things you can do to improve your LCP time:

  • Use a caching plugin. A caching plugin can help to speed up your website by storing static copies of your pages and posts.
  • Minify your CSS and JavaScript files. Minifying your CSS and JavaScript files will reduce their size, which can help them to load faster.
  • Use a content delivery network (CDN). A CDN can help to deliver your website’s content from servers that are located closer to your users, which can improve the loading speed of your website.

By following these tips, you can improve your LCP time and make your website faster and more user-friendly.

Code: https://dev.to/jacksonlewis/how-to-preload-images-in-wordpress-48di