Exclude URLs from being preloaded

If required, URLs and groups of URLs can be excluded from being processed by the Preload feature. You can do this by using the exclusion box or using a filter.

Exclude URLs with the exclusions box

You can exclude URLs from being preloaded by using the Exclude URLs exclusion box, shown below:

preload exclusions box

Note:this will prevent URLs from being preloaded but not cached. To prevent pages from being cached, please check the disable cache for specific pages article.

You can use wildcards to exclude multiple URLs, such as in the examples below:

  • /author/(.*)
  • /color-(.*)
Excluded URLs that were previously being preloaded will be removed from the database the next time Preload runs.

Programmatically exclude URLs from the Preload

You can use the rocket_preload_exclude_urls filter to programmatically exclude URLs from being preloaded.

This can be useful to exclude groups of URLs based on a condition from being preloaded.

Example

With the following snippet you can exclude products with the sold-out category from being added to the preloading queue.

function no_preload_for_sold_out_products( array $regexes ) : array {
    
    $excluded_categories  = array( 
        'sold-out', 
    );
    
    $new_regex =  $_SERVER['REQUEST_URI'];
    
    if(!is_singular('product')) {
        return $regexes;
    }
    
    if ( ( !has_term( $excluded_categories, 'product_cat' ) )  )  {
        
        return $regexes;
    } 
    
    $regexes[] = $new_regex;
    
    return $regexes;
} 

add_filter( 'rocket_preload_exclude_urls', 'no_preload_for_sold_out_products');

You can insert snippets to your site as shown here.

Programmatically limit the Preload to certain URLs

Alternatively, since WP Rocket 3.14.1, you can use the rocket_preload_exclude_urls filter to limit the Preload only to certain URLs.

Example

The following example snippet can be and used to preload only the https://example.org/url1 and https://example.org/url2 URLs, and will exclude any other URL from being preloaded:

add_filter( 'rocket_preload_exclude_urls', function( $regexes, $url ) {
    $inclusion_list = [ 'https://example.org/url1', 'https://example.org/url2', ];
    if ( ! in_array( $url, $inclusion_list, true ) ) {
        $regexes[] = $url;
    }

Programmatically limit the Preload to a sitemap

The rocket_preload_exclude_urls filter can also be used to limit the Preload to a sitemap.

You can use the following helper plugin for this:

📥 Download (.zip): WP Rocket | Only preload URLs in a sitemap

Developers: You can find the code for this plugin on GitHub.

📝 Manual code edit required before use!

  1. After downloading the plugin ZIP file, unzip it and open the PHP file in a text editor.
  2. On line 20, replace https://www.example.com/sitemap_index.xml with your custom sitemap URL.
  3. After making your edits, save the PHP file and re-zip the plugin. 
  4. In your WordPress site, go to Plugins > Add New > Upload Plugin and upload the ZIP file. 
  5. Activate the helper plugin.

Next time Preload runs, it will be limited to the URLs in this sitemap. All the other URLs will be excluded from the Preload, and will be cached only after a visit.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.