This page contains advanced customization options for BigScoots Cache and is intended for developers. If you don't know how to write code, please do not try to tinker with these options as it can lead to breaking your website.
BigScoots Cache not only has an extensive settings page to manage every part of page caching, but also has many actions, filters, and helper methods to allow you to customize your page caching experience.
Configuring the plugin with PHP Constants
The BigScoots Cache plugin has support for the following PHP constants that you can add inside wp-config.php
to overwrite the default values:
BS_CACHE_CURL_TIMEOUT
— timeout in seconds for cURL calls (default: 10)BS_CACHE_HOME_PAGE_SHOWS_POSTS
— if the front page a.k.a. the home page of the website shows the latest posts (default:true
[bool])
Modifying the plugin behaviors with Filters
Filters are the most useful and versatile thing that allows us to modify and fine-tune the plugin’s behavior as needed by simply adding some code snippets. Now, let’s take a look at the filters that are provided by this plugin:
Post Types to ignore from cache purge operation
Filter name: bs_cache_ignored_post_types
— [1 Argument $list_of_post_types
(Array)]
Often WordPress websites have a ton of custom post types but for most of them, cache purge operation is simply not needed (e.g. shop_order
, shop_coupon
, attachment
, etc.). By default, the BigScoots Cache plugin treats all custom post types (except the default excluded custom post types) in the same way unless you specifically tell the plugin to ignore certain post types and not purge the cache for any changes in them. This is what this filter does. Here is a code example of how to utilize this filter:
add_filter('bs_cache_ignored_post_types', function(array $list_of_post_types) : array {
$list_of_post_types[] = 'shop_order';
$list_of_post_types[] = 'shop_coupon';
$list_of_post_types[] = 'attachment';
$list_of_post_types[] = 'some_other_cpt';
return $list_of_post_types;
});
Add more web pages to the related URL list
Filter name: bs_cache_post_related_url_init
— [1 Argument $list_of_urls
(Array)]
This is quite an important filter if the customer's website is using Purge cache for related pages only
option (the default option used by BigScoots Cache) inside the plugin settings under the Cache tab. In this situation, when a customer publishes a new post or updates any existing post, the plugin automatically purges the cache for that article and also purges the cache for the related pages i.e. taxonomy pages (category, tags, etc. up to 10 paginated pages).
Often we see that clients are saying that when we are publishing or updating content, this particular page is not getting purged but I would like to ensure that when I publish or update any content this list of pages is also purged along with the actual content page.
In this situation, this filter comes to the rescue as it allows us to add any list of URLs to be added to the related URL list. Take a look at the code example below:
add_filter('bs_cache_post_related_url_init', function(array $list_of_urls) : array {
// Return an array of the list of urls. This includes the urls we
// want to add along with the ones already present in $listofurls
$list_of_urls[] = home_url('/some-page/');
$list_of_urls[] = home_url('/some-other-page/');
$list_of_urls[] = home_url('/some-other-page-2/');
return $list_of_urls;
});
Alternative use of the same filter while utilizing post ID.
add_filter('bs_cache_post_related_url_init', function(array $list_of_urls, int $post_id) : array {
// Get post type from the post id
$post_type = get_post_type($post_id);
if ($post_type && $post_type === 'job_listing') {
$list_of_urls[] = home_url('/job-board/');
}
return $list_of_urls;
}, 10, 2);
Add Bypass Cache Meta Box option for CPT pages
Filter name: bs_cache_bypass_cache_meta_box_allowed_screen_ids
— [1 Argument - $allowed_screen_ids
(Array)]
Inside the plugin settings, under the Advanced
tab if you set Disable metaboxes on single pages and posts to NO then the plugin will add a meta box section for the page/post edit page where users can select if they want to bypass the cache for that page or post. This is a user-friendly way to Bypass cache for pages/posts instead of using Prevent the following URIs to be cached section under the Cache tab.
But the reason it is disabled by default is that when this option is enabled, it adds an additional DB query. That being said, in some cases, users do ask for it so that can manage what to bypass in a more user-friendly way.
One issue with this option is that by default the meta box is only added to Pages and Posts pages. So, for CPT pages it won’t work. This is where this new filter comes into play. With this filter, custom CPT pages can also be added to the allowed screen IDs for which the meta box will be shown. Let’s look at the code example below:
// Uncomment the code below so that we can see WP_Screen object for that specific page and we can get the value of screen id for that CPT edit page
// var_dump( get_current_screen() );
add_filter('bs_cache_bypass_cache_meta_box_allowed_screen_ids', function(array $allowed_screen_ids) : array {
return [
...$allowed_screen_ids,
'my_custom_post_type_screen_id'
];
});
To use this filter, first, we need to figure out the screen ID of that specific CPT edit page. So, you can uncomment the var_dump()
code to see the screen ID and then add that screen ID to the array.
Bypass Cache
Filter name: bs_cache_cache_bypass
You can use this filter to bypass the BigScoost cache. Here is a code example:
add_filter('bs_cache_cache_bypass', function() : bool {
// Return "true" to bypass page cache operation by BigScoots Cache
return true;
});
Set the CDN Cache TTL dynamically
Filter name: bs_cache_cdn_cache_ttl
— [1 Argument - $cdn_ttl
(Int)]
You can use this filter to modify the value of s-maxage
inside the Cache-Control
header which is mainly used for CDN TTL, to tell BigScoots Cache for how long you want that specific page to be cached at the CDN level. Here’s a code example:
add_filter('bs_cache_cdn_cache_ttl', function(int $cdn_ttl) : int {
if ( is_page('latest') ) { // For the page whish has the slug => 'latest'
$cdn_ttl = 2 * WEEK_IN_SECONDS; // 2 weeks in seconds
} elseif ( is_page('test-page') ) { // For the page whish has the slug => 'test-page'
$cdn_ttl = 5 * MONTH_IN_SECONDS; // 5 months in seconds
}
// Otherwise return the default value
return $cdn_ttl;
});
Set the Browser Cache TTL dynamically
Filter name: bs_cache_browser_cache_ttl
— [1 Argument - $browser_cache_ttl
(Int)]
You can use this filter to modify the value of max-age
inside the Cache-Control
header which is mainly used for Browser Cache TTL, to tell BigScoots Cache for how long you want that specific page to be cached at the browser level. Here’s a code example:
add_filter('bs_cache_browser_cache_ttl', function(int $browser_cache_ttl) : int {
if ( is_page('latest') ) { // For the page whish has the slug => 'latest'
$browser_cache_ttl = 0; // Don't want browser to cache this page
} elseif ( is_page('test-page') ) { // For the page whish has the slug => 'test-page'
$browser_cache_ttl = 10; // Want browser to cache this page for 10 seconds only
}
// Otherwise return the default value
return $browser_cache_ttl;
});
Modify the Cache-Control value dynamically
Filter name: bs_cache_control
— [1 Argument - $cache_control
(String)]
add_filter('bs_cache_control', function(string $cache_control) : string {
if (is_singular('example')) {
$cache_control = 'no-cache, no-store, max-age=0, must-revalidate';
}
return $cache_control;
});
Preload static files leveraging Early Hints
Filter name: bs_cache_early_hint_preloads
— [1 Argument - $preloads
(Array)]
BigScoots Cache has native integration with the Perfmatters WordPress plugin. This native integration allows BigScoots Cache to preload all the items added as "Preload" or "Critical Image" within Perfmatters, to be loaded by leveraging Early Hints technology.
Alternatively, suppose you do not use Perfmatters on your website and still like to leverage Early Hints on your website. In that case, you can use the above filter to pass the critical static assets you would like to be loaded by leveraging Early Hints. Here’s a code example:
/**
* Pass the items to be preloaded via Early Hints
*
* $preloads = [
* [
* "url" => "https://example.com/wp-content/uploads/2021/01/image.jpg",
* "as" => "image"
* ],
* [
* "url" => "https://example.com/wp-content/uploads/2021/01/font.woff",
* "as" => "font"
* ],
* [
* "url" => "https://example.com/wp-content/uploads/2021/01/script.js",
* "as" => "script"
* ],
* [
* "url" => "https://example.com/wp-content/uploads/2021/01/style.css",
* "as" => "style"
* ]
* ];
**/
add_filter('bs_cache_early_hint_preloads', function(array $preloads) : array {
if (is_page('contact')) {
$preloads[] = [
"url" => "https://example.com/wp-content/uploads/2021/01/contact.js",
"as" => "script"
];
} elseif (is_page('about')) {
$preloads[] = [
"url" => "https://example.com/wp-content/uploads/2021/01/about.js",
"as" => "script"
];
} elseif (is_page('portfolio')) {
$preloads[] = [
"url" => "https://example.com/wp-content/uploads/2021/01/portfolio.css",
"as" => "style"
];
}
return $preloads;
});
Disable Related URLs purge
Filter name: bs_cache_disable_related_urls_purge
— [2 Arguments - $disable_related_urls_purge
(Bool) & $post_id
(Int)]
Please Note: It is highly discouraged to use this filter on any site unless there is any special reason for it. Cause when this filter is enabled, the plugin won’t purge the cache for any related URLs like the home page, taxonomy, author, or other archive pages. So, make sure of this outcome before enabling this filter.
By default, BigScoots Cache purges the cache for the post being published or updated and its related URLs, such as taxonomy, author, category, home page, etc. This approach ensures that changes are reflected in all key places when content is published or updated.
However, if a site doesn’t want to purge the related URLs and only the cache for the post that is being published or updated, it can use this filter to disable cache purging for the related URLs.
Here’s a code example:
// Disable Related URLs purge globally for the entire site
add_filter('bs_cache_disable_related_urls_purge', function() : bool {
return true;
});
// Disable Related URLs purge selectively for the certain post ids
add_filter('bs_cache_disable_related_urls_purge', function(bool $disable_related_urls_purge, int $post_id) : bool {
if ( $post_id === 10 || $post_id === 20 ) {
$disable_related_urls_purge = true;
}
return $disable_related_urls_purge;
}, 10, 2);
Add additional URL Patterns (RegEx) to the Speculation Rules exclude list
Filter name: bs_cache_speculation_href_exclude_paths
— [1 Argument - $href_exclude_paths
(Array)]
add_filter('bs_cache_speculation_href_exclude_paths', function(array $href_exclude_paths) : array {
$href_exclude_paths[] = '/account*'; // e.g. /account, /accounts, /account/some-page/ etc...
$href_exclude_paths[] = '/test-post/'; // e.g. just /test-post/, /test-post/?foo=bar etc...
$href_exclude_paths[] = '/*\\?*(^|&)(foo|loo|poo)=*'; // e.g. any URL with `foo=` or `loo=` or `poo=` query param in it
return $href_exclude_paths;
});
Change Speculation Mode from Prerender (default) to Prefetch
Filter name: bs_cache_speculation_mode
— [1 Argument - $mode
(String)]
add_filter('bs_cache_speculation_mode', function(string $mode) : string {
$mode = 'prefetch'; // changing speculation mode from default Prerender to Prefetch
return $mode;
});
Modify the speculation rule any way you see fit before it gets added
Filter name: bs_cache_speculation_rules
— [1 Argument - $rules
(Array)]
add_filter('bs_cache_speculation_rules', function(array $rules) : array {
// Modify the speculation rules as you see fit
// Finally return the speculation rules
return $rules;
});
Disable OPCache purge that happens via BigScoots Cache
Filter name: bs_cache_disable_clear_opcache
add_filter('bs_cache_disable_clear_opcache', fn() => true);
Disable Object Cache purge that happens via BigScoots Cache
Filter name: bs_cache_disable_clear_object_cache
add_filter('bs_cache_disable_clear_object_cache', fn() => true);
Modifying the plugin behaviors with Actions
Actions are called when a certain operation has been completed or before starting those operations. Now, let’s take a look at the actions that are provided by this plugin:
Fired when whole caches are purged
Action name: bs_cache_purge_all
add_action('bs_cache_purge_all', function() : void {
// BigScoots Cache has purged the cache completely.
// Now if you would like to run some custom code
// after the cache has been purged, feel free to do that...
});
Fired when caches for specific URLs are purged
Action name: bs_cache_purge_urls
— [1 Argument: $urls
(Array)]
add_action('bs_cache_purge_urls', function(array $urls) : void {
// BigScoots Cache has purged the list of URLs.
// You can access the list of URLs that has been purged
// by BigScoots Cache via the $urls variable.
// now if you would like to run some custom code after
// the cache purge, feel free to do that here...
});
Fired before purging the whole BigScoots cache (purge everything)
Action name: bs_cache_cf_purge_whole_cache_before
If a user has selected the option to purge cache for the entire website (including static files), before executing this purge everything operation, this action is called. Here is an example code for using this action:
add_action('bs_cache_cf_purge_whole_cache_before', function() : void {
// About to purge the entire cache of the website (including
// static assets). So, if you need to execute some custom code
// at this point, you can do that here...
});
Fired after purging the whole BigScoots cache (purge everything)
Action name: bs_cache_cf_purge_whole_cache_after
If a user has selected the option to purge cache for the entire website (including static files), after executing this purge everything operation, this action is called. Here is an example code for using this action:
add_action('bs_cache_cf_purge_whole_cache_after', function() : void {
// Purged the entire cache of the website (including static assets).
// So, if you need to execute some custom code at this point,
// you can do that here...
});
Fired before purging the BigScoots cache for specific URLs only
Action name: bs_cache_cf_purge_cache_by_urls_before
— [1 Argument: $urls
(Array)]
If a user has selected the option to purge cache for the entire website (excluding static files), before executing this purge everything operation, this action is called. Here is an example code for using this action:
add_action('bs_cache_cf_purge_cache_by_urls_before', function(array $urls) : void {
// Before purging the cache of the selected URLs (excluding static assets).
// So, if you need to execute some custom code at this point,
// you can do that here...
// You can also access the list of URLs cache has been purged for by
// using the $urls variable.
});
Fired after purging the BigScoots cache for specific URLs only
Action name: bs_cache_cf_purge_cache_by_urls_after
— [1 Argument: $urls
(Array)]
If a user has selected the option to purge cache for the entire website (excluding static files), after executing this purge everything operation, this action is called. Here is an example code for using this action:
add_action('bs_cache_cf_purge_cache_by_urls_after', function(array $urls) : void {
// Purged the cache of the selected URLs (excluding static assets).
// So, if you need to execute some custom code at this point,
// you can do that here...
// You can also access the list of URLs cache has been purged for by
// using the $urls variable.
});
Purging the OPCache programmatically
If needed the server OPCache can also be purged programmatically by executing the following code:
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_opcache') ) {
\BigScoots_Cache::clear_opcache();
}
Purging the Object Cache programmatically
If needed the server OPCache can also be purged programmatically by executing the following code:
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_object_cache') ) {
\BigScoots_Cache::clear_object_cache();
}
Purging the cache programmatically
The BigScoots cache can be purged programmatically by executing the below codes in the functions.php
file or any third-party plugins can use the following code to purge BigScoots Cache inside their plugin when they feel that the cache needs to be purged for the website.
The following code will allow users to purge BigScoots Cache in the following manners:
Purge everything for the entire domain
Purge cache for a specific Post ID
Purge cache for a specific Post ID and its related pages (e.g. home page, taxonomy, category, author, and other pages)
Purge cache for a list of Post IDs (must need to passed as an integer array)
Purge cache for a list of Post IDs (must need to passed as an integer array) and their related pages (e.g. home page, taxonomy, category, author, and other pages)
Purge cache for list of URLs — can pass an array of URLs to be purged
Here is the code example of how to implement the code:
// Purge Everything for the domain
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache('purge_everything');
}
// Clear Cache for the specific Post ID BUT do not clear cache for related pages
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache(85);
}
// Clear Cache for the specific Post ID & Related Pages (e.g. home page, taxonomy, category, author and other pages)
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache(85, true);
}
// Clear Cache for a list of Post IDs BUT do not clear cache for related pages
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache([85, 96, 102]);
}
// Clear Cache for a list of Post IDs & Related Pages (e.g. home page, taxonomy, category, author and other pages)
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache([85, 96, 102], true);
}
// Clear Cache for the specific set of URLs
if ( class_exists('BigScoots_Cache') && method_exists('BigScoots_Cache', 'clear_cache') ) {
\BigScoots_Cache::clear_cache(['https://learn-wpo.com/test/', 'https://learn-wpo.com/test-saumya/', 'https://learn-wpo.com/category/uncategorized/']);
}