Filters and PHP constants

The Servebolt Optimizer-plugin and its behaviour can be altered by using filters and PHP constants.

Usage and examples

The concept of using filters and PHP constants are very similar – they are used to change values and behaviours. But they do have their differences – please see below for an example of their difference.

PHP constants are useful for specifying a single value:

define('SERVEBOLT_FPC_CACHE_TIME', 1200); // Set Full Page Cache time to 20 minutes
define('SB_CF_REQUEST_DEBUG', true); // Debug requests to Cloudflare API

Filters are useful if you would like to write some logic as well as changing a value:

add_filter('sb_optimizer_display_admin_bar_menu_by_user_capabilities', function($display_admin_bar) {
    if ( ! current_user_can('my_custom_capability') ) {
        return false;
    }
    return $display_admin_bar;
});

Read more about filters in documentation of WordPress.

There are also some built in functions that are handy when working with filters:

  • __return_true
  • __return_false
  • __return_zero
  • __return_empty_array
  • __return_null
  • __return_empty_string

An example of this:

add_filter('sb_optimizer_automatic_purge_on_post_save', '__return_true');
add_filter('sb_optimizer_max_number_of_urls_to_be_purged', '__return_zero');

PHP constants

SERVEBOLT_FPC_CACHE_TIME (integer)

Used to set the “Expires”-header. Defaults to 600 seconds. Only active for Servebolt-customers when using full page cache.

SERVEBOLT_BROWSER_CACHE_TIME (integer)

Used to set the “max-age”-parameter in the “Cache-Control”-header. Defaults to 600 seconds. Only active for Servebolt-customers when using full page cache.

SB_CF_REQUEST_DEBUG (boolean)

Whether to debug Cloudflare API request data to the error log.

SERVEBOLT_CF_PURGE_CRON (boolean)

Whether to use the WP cron to purge the cache (the alternative is that the cache purge happens immediately, without any queue).

SERVEBOLT_CF_PURGE_CRON_PARSE_QUEUE (boolean)

Whether to execute cache purge on items in the queue. Can be used to only queue up items for cache purge, but not execute the cache purge.

SERVEBOLT_CF_PURGE_CRON_CLEANER_ACTIVE (boolean)

Whether to clean the cache purge queue items that are older than 7 days (amount of days can be changed with filter “sb_optimizer_clean_cache_purge_queue_time_threshold”).

SERVEBOLT_CF_IMAGE_RESIZE_ACTIVE (boolean)

Whether to activate the Cloudflare Image Resize feature (beta).

Filters

Under you will find a full list of all available filters in the plugin and how they will affect the behaviour:

sb_optimizer_add_gutenberg_plugin_menu

Whether to display the plugin menu in the Gutenberg editor.

function filter_function_name($boolean) {
    return $boolean;
}
add_filter('sb_optimizer_add_gutenberg_plugin_menu', 'filter_function_name', 10, 1);

sb_optimizer_fpc_should_debug_headers

Whether to print debug headers related to the full page cache (only for Servebolt-hosted sites).

function filter_function_name($boolean) {
    return $boolean;
}
add_filter('sb_optimizer_fpc_should_debug_headers', 'filter_function_name', 10, 1);

sb_optimizer_fpc_woocommerce_pages_no_cache_bool

In full page cache context – use this boolean to override whether the current request is to a WooCommerce page that should not be cached.

function filter_function_name($boolean) {
    // Determine whether we are looking at a WooCommerce page that should not be cached
    return $boolean;
}
add_filter('sb_optimizer_fpc_woocommerce_pages_no_cache_bool', 'filter_function_name', 10, 1);

sb_optimizer_fpc_woocommerce_pages_cache_bool

In full page cache context – use this boolean to override whether the current request is to a WooCommerce page that should be cached.

function filter_function_name($boolean) {
    // Determine whether we are looking at a WooCommerce page that should be cached
    return $boolean;
}
add_filter('sb_optimizer_fpc_woocommerce_pages_cache_bool', 'filter_function_name', 10, 1);

sb_optimizer_display_admin_bar_menu

Whether to display the plugin admin bar menu item.

function filter_function_name($boolean) {
    // Determine if we should display the plugins admin bar menu
    return $boolean;
}
add_filter('sb_optimizer_display_admin_bar_menu', 'filter_function_name', 10, 1);

sb_optimizer_display_admin_bar_menu_by_user_capabilities

Use this filter to control which users (based on capabilities) that can see the admin bar.

function filter_function_name($boolean) {
    // Determine if we should display the plugins admin bar menu based on a capability
    return current_user_can('my_custom_capability');
}
add_filter('sb_optimizer_display_admin_bar_menu_by_user_capabilities', 'filter_function_name', 10, 1);

sb_optimizer_display_network_super_admin_menu

Whether to display the network admin menu.

function filter_function_name($boolean) {
    // Determine if we should display the network admiin menu
    return $boolean;
}
add_filter('sb_optimizer_display_network_super_admin_menu', 'filter_function_name', 10, 1);

sb_optimizer_display_subsite_menu

Whether to display the subsite admin menu (if multisite).

function filter_function_name($boolean) {
    // Determine if we should display the admin menu on a subsite in a multisite network
    return $boolean;
}
add_filter('sb_optimizer_display_subsite_menu', 'filter_function_name', 10, 1);

sb_optimizer_display_single_site_admin_menu

Whether to display the admin menu on a single site.

function filter_function_name($boolean) {
    // Determine if we should display the admin menu
    return $boolean;
}
add_filter('sb_optimizer_display_single_site_admin_menu', 'filter_function_name', 10, 1);

sb_optimizer_purge_item_list_limit

The number of items to display in the cache purge list.

function filter_function_name($limit) {
    return 50; // Only display 50 items in the purge queue list
}
add_filter('sb_optimizer_purge_item_list_limit', 'filter_function_name', 10, 1);

sb_optimizer_log_file_path

The path of the log file – only available when hosted in Servebolt.

function filter_function_name($log_file_path) {
    return '/custom/path/to/logfile.log'; // Override the path to the log file to inspect
}
add_filter('sb_optimizer_log_file_path', 'filter_function_name', 10, 1);

sb_optimizer_cf_cache_form_validation_active

Whether to use JavaScript-based validation when editing the Cloudflare cache configuration.

function filter_function_name($boolean) {
    return false; // Disable JavaScript validation for Cloudflare cache configuration form
}
add_filter('sb_optimizer_cf_cache_form_validation_active', 'filter_function_name', 10, 1);

sb_optimizer_should_generate_other_urls

Whether to generate other URLs (archives, front page) when busting Cloudflare cache for a WP object.

function filter_function_name($boolean) {
    return false; // When purging cache, don't purge other related URLs like archives etc.
}
add_filter('sb_optimizer_should_generate_other_urls', 'filter_function_name', 10, 1);

sb_optimizer_alter_urls_for_cache_purge_object

Use this filter to alter the URLs when purging Cloudflare cache.

function filter_function_name($urls, $purge_item_id, $purge_item_type) {
	$urls[] = 'https://my-custom-purge.com/some-path/';
    return $urls;
}
add_filter('sb_optimizer_alter_urls_for_cache_purge_object', 'filter_function_name', 10, 3);

sb_optimizer_disable_automatic_purge

Whether to disable the automatic Cloudflare cache purge feature (for example when saving a post etc.).

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_disable_automatic_purge', 'filter_function_name', 10, 1);

sb_optimizer_automatic_purge_on_post_save

Whether to purge Cloudflare purge automatically on post update.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_automatic_purge_on_post_save', 'filter_function_name', 10, 1);

sb_optimizer_automatic_purge_on_comment

Whether to purge Cloudflare purge automatically on comment post.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_automatic_purge_on_comment', 'filter_function_name', 10, 1);

sb_optimizer_automatic_purge_on_comment_approval

Whether to purge Cloudflare purge automatically on comment approval.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_automatic_purge_on_comment_approval', 'filter_function_name', 10, 1);

sb_optimizer_automatic_purge_on_term_save

Whether to purge Cloudflare purge automatically on term update.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_automatic_purge_on_term_save', 'filter_function_name', 10, 1);

sb_optimizer_should_purge_term_cache

Use this filter to override the check that decides whether we should purge term cache in Cloudflare or not.

function filter_function_name($boolean, $term_id) {
	return $boolean;
}
add_filter('sb_optimizer_should_purge_term_cache', 'filter_function_name', 10, 2);

sb_optimizer_should_purge_post_cache

Use this filter to override the check that decides whether we should purge post cache in Cloudflare or not.

function filter_function_name($boolean, $post_id) {
	return $boolean;
}
add_filter('sb_optimizer_should_purge_post_cache', 'filter_function_name', 10, 2);

sb_optimizer_prevent_cache_purge_on_unapproved_comments

Whether we should purge cache on unapproved comment post.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_prevent_cache_purge_on_unapproved_comments', 'filter_function_name', 10, 1);

sb_optimizer_comment_approved_cache_purge

Whether a comment is considered as approved on comment post in the context of our automatic cache purge.

function filter_function_name($boolean, $comment_data) {
	return $boolean;
}
add_filter('sb_optimizer_comment_approved_cache_purge', 'filter_function_name', 10, 2);

sb_optimizer_cf_image_resize_alter_srcset

Whether to alter the srcset-attribute when using the Cloudflare image resize feature.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_cf_image_resize_alter_srcset', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_alter_src

Whether to alter the src-attribute when using the Cloudflare image resize feature.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_cf_image_resize_alter_src', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_alter_intermediate_sizes

Whether to affect the generation of image sizes when uploading an image.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_cf_image_resize_alter_intermediate_sizes', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_always_create_sizes

When generating images and using the Cloudflare image resize feature, then use this filter to decide which image sizes that should always be created files for.

function filter_function_name($sizes) {
	// Make changes to $sizes
	return $sizes;
}
add_filter('sb_optimizer_cf_image_resize_always_create_sizes', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_upscale_images

Whether to use upscale the images when using the Cloudflare image resize-feature.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_cf_image_resize_upscale_images', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_max_width

Cloudflare image resize max width.

function filter_function_name($boolean) {
	return 1920; // Override max width for Cloudflare image resize
}
add_filter('sb_optimizer_cf_image_resize_max_width', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_max_height

Cloudflare image resize max height.

function filter_function_name($boolean) {
	return 1080; // Override max height for Cloudflare image resize
}
add_filter('sb_optimizer_cf_image_resize_max_height', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_url

The image URL after modification when using the Cloudflare image resize feature.

function filter_function_name($url, $original_url, $cdi_uri, $cf_params, $cf_parameter_string) {
	// Do something with $url
	return $url;
}
add_filter('sb_optimizer_cf_image_resize_url', 'filter_function_name', 10, 5);

sb_optimizer_cf_image_resize_default_params_additional

The additional URL parameters (before arguments are merged with default arguments) used when modifying the URL to use Cloudflare image resize.

function filter_function_name($parameters) {
	// Do something with $parameters
	return $parameters;
}
add_filter('sb_optimizer_cf_image_resize_default_params_additional', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_default_params

The default URL parameters (before arguments are merged with additional arguments) used when modifying the URL to use Cloudflare image resize.

function filter_function_name($parameters) {
	// Do something with $parameters
	return $parameters;
}
add_filter('sb_optimizer_cf_image_resize_default_params', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_default_params_concatenated

The URL parameters (after arguments are merged with default arguments) used when modifying the URL to use Cloudflare image resize.

function filter_function_name($parameters) {
	// Do something with $parameters
	return $parameters;
}
add_filter('sb_optimizer_cf_image_resize_default_params_concatenated', 'filter_function_name', 10, 1);

sb_optimizer_cf_image_resize_upscale_dimensions

When using the Cloudflare image resize feature – use this filter to adjust the upscale dimensions of the image.

function filter_function_name($dimensions) {
	// Do something with $dimensions
	return $dimensions;
}
add_filter('sb_optimizer_cf_image_resize_upscale_dimensions', 'filter_function_name', 10, 1);

sb_optimizer_should_clean_cache_purge_queue

Whether to run automatic cache purge queue cleaning of items older than 7 days.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_should_clean_cache_purge_queue', 'filter_function_name', 10, 1);

sb_optimizer_clean_cache_purge_queue_time_threshold

The timestamp (uses the timezone set in the WP settings) used to determine if a cache purge queue item is old and should be deleted.

function filter_function_name($timestamp) {
	return $timestamp;
}
add_filter('sb_optimizer_clean_cache_purge_queue_time_threshold', 'filter_function_name', 10, 1);

sb_optimizer_should_purge_cache_queue

Whether the system should parse the cache purge queue and execute the cache purge.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_should_purge_cache_queue', 'filter_function_name', 10, 1);

sb_optimizer_cf_api_request_debug

Whether to debug the request to the Cloudflare API.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_cf_api_request_debug', 'filter_function_name', 10, 1);

sb_optimizer_max_number_of_urls_to_be_purged

Limit the number of URLs being sent for purging in the Cloudflare API. This is due to limitations in the Cloudflare API – maximum 30 URLs per purge request. Using this filter is recommended, but it will prevent an error until a better solution is implemented.

function filter_function_name($limit) {
	return 30; // Set an upper limit for the number of URLs being purged in Cloudflare cache
}
add_filter('sb_optimizer_max_number_of_urls_to_be_purged', 'filter_function_name', 10, 1);

sb_optimizer_urls_to_be_purged

The array of URLs to be purged in Cloudflare.

function filter_function_name($urls) {
	// Do something with $urls
	return $urls;
}
add_filter('sb_optimizer_urls_to_be_purged', 'filter_function_name', 10, 1);

sb_optimizer_mcrypt_key

The mcrypt key/secret used to store options.

function filter_function_name($key) {
	return 'custom-key'; // Use custom encryption key for Mcrypt
}
add_filter('sb_optimizer_mcrypt_key', 'filter_function_name', 10, 1);

sb_optimizer_openssl_keys

The OpenSSL key/secret used to store options.

function filter_function_name($keys) {
	// Do something with $keys
	return $keys;
}
add_filter('sb_optimizer_openssl_keys', 'filter_function_name', 10, 1);

sb_optimizer_evaluate_multidomain_setup

When checking if a multisite contains multiple domains, use this filter to alter the result of the domain lookup.

function filter_function_name($domains) {
	// Do something with $domains
	return $domains;
}
add_filter('sb_optimizer_evaluate_multidomain_setup', 'filter_function_name', 10, 1);

sb_optimizer_evaluate_multidomain_setup_conclusion

A boolean value concluding whether the multisite-setup contains multiple domains.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_evaluate_multidomain_setup_conclusion', 'filter_function_name', 10, 1);

sb_optimizer_get_option_(option_name)

Filter for value of option.

function filter_function_name($api_token) {
	return 'my-custom-api-token;
}
add_filter('sb_optimizer_get_option_cf_api_token', 'filter_function_name', 10, 1);

sb_optimizer_get_blog_option_(option_name)

Filter for value of blog option.

function filter_function_name($api_token) {
	return 'my-custom-api-token;
}
add_filter('sb_optimizer_get_blog_option_cf_api_token', 'filter_function_name', 10, 1);

The arguments passed to the function “paginate_links” when generating pagination links in the context of the Cloudflare cache purge-feature.

function filter_function_name($args) {
	return $args;
}
add_filter('sb_paginate_links_as_array_args', 'filter_function_name', 10, 1);

sb_optimizer_skip_generic_optimizations

Whether to apply the generic optimizations.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_skip_generic_optimizations', 'filter_function_name', 10, 1);

sb_optimizer_ajax_user_allowed

Used to control the allowance (user capabilities) to send AJAX request to the AJAX endpoints of this plugin.

function filter_function_name($user_allowed) {
	if ( ! current_user_can('my_custom_capability') ) {
		return false;
	}
	return $user_allowed;
}
add_filter('sb_optimizer_ajax_user_allowed', 'filter_function_name', 10, 1);

sb_optimizer_skip_pages_needed_request

When busting cache witt Cloudflare, when looking up URLs to an archive and the number of pages in pagination – use this boolean to disable the pagination lookup feature (this feature can be slow on slow sites). When set to false then the number of pages will revert to 250, but this number can be overridden with the filter “sb_optimizer_pages_needed_override”.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_skip_pages_needed_request', 'filter_function_name', 10, 1);

sb_optimizer_pages_needed_override

When busting cache witt Cloudflare, this is the number of pages-number used when generating the paginated URLs to an archive. Used when the filter “sb_optimizer_skip_pages_needed_request” is set to false. Defaults to 250.

function filter_function_name($pages_needed) {
	return 30;
}
add_filter('sb_optimizer_pages_needed_override', 'filter_function_name', 10, 1);

sb_optimizer_record_max_num_pages_filter_hook

The filter used to “record” the number of pages-number when generating paginated archive URLs.

function filter_function_name($hook) {
	// Do something with $hook
	return $hook;
}
add_filter('sb_optimizer_record_max_num_pages_filter_hook', 'filter_function_name', 10, 1);

sb_optimizer_site_iteration

The filter used to “record” the number of pages-number when generating paginated archive URLs.

function filter_function_name($sites) {
	// Do something with $sites
	return $sites;
}
add_filter('sb_optimizer_site_iteration', 'filter_function_name', 10, 1);

sb_optimizer_add_version_parameter_to_asset_src

Whether to add version parameter to assets URLs.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_add_version_parameter_to_asset_src', 'filter_function_name', 10, 1);

sb_optimizer_version_parameter_name

The string used to create the version parameter for asset URLs.

function filter_function_name($parameter_name, $file_path) {
	return $parameter_name;
}
add_filter('sb_optimizer_version_parameter_name', 'filter_function_name', 10, 2);

sb_optimizer_asset_base_path

The base path used when converting an asset URL to asset path.

function filter_function_name($base_path, $src, $parsed_url) {
	return $base_path;
}
add_filter('sb_optimizer_asset_base_path', 'filter_function_name', 10, 3);

sb_optimizer_asset_parsed_url_path

The URL path used when converting an asset URL to asset path.

function filter_function_name($src) {
	return $src;
}
add_filter('sb_optimizer_asset_parsed_url_path', 'filter_function_name', 10, 1);

sb_optimizer_add_version_parameter_to_script_src

Whether to add version parameter to asset script URLs.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_add_version_parameter_to_script_src', 'filter_function_name', 10, 1);

sb_optimizer_add_version_parameter_to_style_src

Whether to add version parameter to asset style URLs.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_add_version_parameter_to_style_src', 'filter_function_name', 10, 1);

sb_optimizer_add_version_parameter_to_script_src_(handle)

Whether to add version parameter to the asset script URL for a certain handle.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_add_version_parameter_to_script_src_my_site_script_handle', 'filter_function_name', 10, 1);

sb_optimizer_add_version_parameter_to_style_src_(handle)

Whether to add version parameter to the asset style URL for a certain handle.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_add_version_parameter_to_style_src_my_site_style_handle', 'filter_function_name', 10, 1);

sb_optimizer_asset_url_to_path_conversion

The converted path of an asset URL.

function filter_function_name($src, $parsed_url) {
	return $src;
}
add_filter('sb_optimizer_asset_url_to_path_conversion', 'filter_function_name', 10, 2);

sb_optimizer_asset_url_to_path_conversion_(handle)

The converted path of an asset URL for a certain handle.

function filter_function_name($src, $parsed_url) {
	return $src;
}
add_filter('sb_optimizer_asset_url_to_path_conversion_my_site_style_handle', 'filter_function_name', 10, 2);

sb_optimizer_throttle_queue_items_on_cron_purge

When using the cache purge cron feature – set this to true to throttle the amount of items that gets parsed at a time.

function filter_function_name($boolean) {
	return $boolean;
}
add_filter('sb_optimizer_throttle_queue_items_on_cron_purge', 'filter_function_name', 10, 1);

sb_optimizer_throttle_queue_max_items

Use this filter in relation to the filter “sb_optimizer_throttle_queue_items_on_cron_purge” to control the amount of items to be included in each purge request.

function filter_function_name($limit) {
	return $limit;
}
add_filter('sb_optimizer_throttle_queue_max_items', 'filter_function_name', 10, 1);

sb_optimizer_should_load_common_assets

Whether to load the common assets (scripts and styling).

function filter_function_name($boolean, $type) {
	return $boolean;
}
add_filter('sb_optimizer_should_load_common_assets', 'filter_function_name', 10, 2);

Give us your feedback on this article