Last updated on September 25, 2023

Enable Caching for Specific WordPress Pages

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Cache specific pages for speed.

Enabling caching for specific WordPress pages can greatly improve the performance and speed of your website. By caching certain pages, you can store their generated HTML output and serve it to visitors without the need to regenerate the content every time. This is particularly useful for pages that don’t change frequently, such as static pages or archives.

To enable caching for specific WordPress pages, you can use the wp_cache_add() function provided by WordPress. This function allows you to add a key-value pair to the cache, where the key represents the unique identifier for the cached content, and the value is the actual content you want to cache.

Here’s an example of how you can enable caching for a specific WordPress page:

function wpsnippets_enable_page_caching() {
    if ( is_page( 'about' ) ) {
        $cache_key = 'about_page_content';
        $cached_content = wp_cache_get( $cache_key );

        if ( false === $cached_content ) {
            // Generate the content for the 'about' page
            ob_start();
            // Your code to generate the page content goes here
            $page_content = ob_get_clean();

            // Cache the generated content for 1 hour
            wp_cache_add( $cache_key, $page_content, '', 3600 );
        } else {
            echo $cached_content;
            exit;
        }
    }
}
add_action( 'template_redirect', 'wpsnippets_enable_page_caching' );

In this example, we’re using the template_redirect action hook to check if the current page being accessed is the ‘about’ page. If it is, we proceed with enabling caching for that page.

First, we check if the page content is already cached by using the wp_cache_get() function. If the content is not found in the cache, we generate the page content using the appropriate code and store it in the cache using the wp_cache_add() function. We set the cache expiration time to 1 hour (3600 seconds) in this example, but you can adjust it to your needs.

If the page content is found in the cache, we retrieve it and output it to the visitor using echo. We also exit the script to prevent further processing.

By implementing this code snippet, you can selectively enable caching for specific WordPress pages, improving the performance and speed of your website.

Examples

Example 1: Enable caching for a specific WordPress page using a plugin

This example demonstrates how to enable caching for a specific WordPress page using a caching plugin like WP Super Cache.

/**
 * Enable caching for a specific WordPress page.
 */
function wpsnippets_enable_caching_for_specific_page() {
    if ( is_page( 'about' ) ) {
        wp_cache_post_id_headers( get_queried_object_id() );
    }
}
add_action( 'wp', 'wpsnippets_enable_caching_for_specific_page' );

In this code example, we use the is_page() function to check if the current page is the “about” page. If it is, we call the wp_cache_post_id_headers() function to enable caching for that specific page.

Example 2: Enable caching for a specific WordPress page using a custom function

This example demonstrates how to enable caching for a specific WordPress page using a custom PHP function.

/**
 * Enable caching for a specific WordPress page.
 */
function wpsnippets_enable_caching_for_specific_page() {
    if ( is_page( 'contact' ) ) {
        $page_id = get_queried_object_id();
        wp_cache_post_id_headers( $page_id );
    }
}
add_action( 'wp', 'wpsnippets_enable_caching_for_specific_page' );

In this code example, we use the is_page() function to check if the current page is the “contact” page. If it is, we retrieve the page ID using the get_queried_object_id() function and then call the wp_cache_post_id_headers() function to enable caching for that specific page.

Example 3: Enable caching for multiple specific WordPress pages

This example demonstrates how to enable caching for multiple specific WordPress pages using a custom function.

/**
 * Enable caching for specific WordPress pages.
 */
function wpsnippets_enable_caching_for_specific_pages() {
    $pages = array( 'about', 'contact', 'services' );
    if ( is_page( $pages ) ) {
        $page_id = get_queried_object_id();
        wp_cache_post_id_headers( $page_id );
    }
}
add_action( 'wp', 'wpsnippets_enable_caching_for_specific_pages' );

In this code example, we define an array of page slugs that we want to enable caching for. We use the is_page() function with the array of page slugs to check if the current page is one of the specified pages. If it is, we retrieve the page ID using the get_queried_object_id() function and then call the wp_cache_post_id_headers() function to enable caching for that specific page.

Last updated on September 25, 2023. Originally posted on October 4, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *