Customize Preload sitemaps and priority

If you need to customize the way Preload works with sitemaps and their priority in the process, you can use the rocket_sitemap_preload_list and rocket_preload_order filters as shown below.

Heads up! This guide includes advanced configuration of the Preload feature. In most cases, the default behavior of this feature is the adequate, and customization is not required.

Tip: You can add the following code snippets as shown in this guide.

Preload a custom sitemap

You can use the following snippet if you need to preload URLs in a custom sitemap instead of the compatible sitemaps.

function wprocket_preload_only_sitemap() {
    return ['https://domain.com/wp-content/files/custom-sitemap.xml'];
}
add_filter( 'rocket_sitemap_preload_list', 'wprocket_preload_only_sitemap', PHP_INT_MAX );

With this approach, the Preload will run on the URLs found in your sitemap. 

Please note that other URLs will still be added to the Preload queue if they receive a visit.

Preload only a custom sitemap

You can use the following helper plugin if you need to limit the Preload to run only on the URLs in a custom sitemap, and exclude all the other URLs.

📥 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!
Note: This helper plugin only works for a single sitemap XML file.

  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, but can still be cached if they receive a visit.

Preload a group of sitemaps

In order to preload the URLs found in multiple custom sitemaps, you can use the code below.

function wprocket_preload_only_sitemaps_multiple() {
    return ['https://domain.com/wp-content/files/custom-sitemap.xml', 'https://domain.com/wp-content/files/custom-sitemap-2.xml'];
}
add_filter( 'rocket_sitemap_preload_list', 'wprocket_preload_only_sitemaps_multiple', PHP_INT_MAX );

In this approach, URLs found in the custom sitemaps will be preloaded, and other URLs will be added to the Preload queue if they receive a visit.

Preload additional custom sitemaps

Use the following snippet to ensure specific sitemaps are included in the preloading process, in addition to the compatible sitemaps.

function wprocket_preload_add_custom_sitemaps ( $sitemaps ){
    
    $sitemaps[] = 'https://domain.com/wp-content/files/custom-sitemap.xml';
    $sitemaps[] = 'https://domain.com/wp-content/files/custom-sitemap-2.xml';
    
    return $sitemaps;
}
add_filter( 'rocket_sitemap_preload_list', 'wprocket_preload_add_custom_sitemaps', PHP_INT_MAX );

In this approach, URLs found in the custom sitemaps and URLs found in compatible sitemaps will be preloaded, but also, other URLs will be added to the Preload queue if they are visited.

Note: when using any of the above snippets, that include the rocket_sitemap_preload_list hook, Preload has to be disabled/re-enabled for the changes to be applied. 

Use-cases of preloading custom sitemaps

Below is the list of possible scenarios where you need to customize how the sitemaps Preload works:

  • You have a big ecommerce website, and need to limit the pages to be added to the preload queue.
  • Only the pages with critical content of your site need to be preloaded. You create a sitemap (with, for example all categories, landing pages and products) and use the first customization in the current article.
  • Some pages are not present in the compatible sitemaps. In this case, you can create a custom sitemap with those URLs, and add them using the third method.

Customize the priority of Preload

The  wp_wpr_rocket_cache table is used to track the progress of the Preload, each added URL has a unique id , which is an auto-increment value.

The  rocket_preload_order filter can be used to make the Preload run on URLs according to their id instead of their modified value.

Here's the snippet you should use:

add_filter( 'rocket_preload_order', function( $order ){
    return true;
}, 1);

Note: For the snippet to take effect, you'd need to go to Settings → Permalinks, and click on Save Changes, in your WordPress dashboard.

Use-case of customizing the priority of Preload

The default order of  Preload is sufficient in most cases, but some pages can be preloaded first in case:

  • You have a big site and you need to make sure critical pages are always preloaded first. So, you need to combine the usage of custom sitemaps with the priority option.

For reference, when using the custom sitemaps option, the listed sitemaps will be fetched first, their URLs will have the lowest id, and therefore, those URLs will be prioritized in the preloading process. But the actual order used to process the queue is by modified and not the order by which it was entered to the table.

This means that although the initial preload will likely respect the order, over time, if not using the filter to adjust the order, as pages are cleared from the cache individually, that order will change.

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