Home
/
WordPress Tutorials
/
Speed Optimizer Plugin Tutorial
/
How to use Speed Optimizer Custom Filters

How to use Speed Optimizer Custom Filters

In today’s digital world, your website’s performance can make or break your success. A website that loads quickly and runs smoothly can significantly improve user experience, boosting your site’s traffic, engagement, and conversion rates.

Speed Optimizer is an innovative solution that enhances your website’s performance. This powerful tool provides a suite of custom filters that let you fine-tune your website‘s caching, image optimization, and script handling, among other things.

With these filters, you can control cache purge permissions, limit preheated URLs, add cache bypass cookies, exclude specific URLs from being cached, customize maximum image width, and much more.

In this tutorial, you will find a detailed guide on how to use Speed Optimizer’s Custom Filters to supercharge your website’s performance, so read on.

Control which user roles have permission to do a manual cache purge

For advanced customization, we’ve incorporated a filter that allows you to control which user roles have access to flush the cache using the Purge SG Cache button.

Here’s an example of code that you can add to your functions.php file to further tailor your caching experience:

add_filter( 'sgo_purge_button_capabilities', 'sgo_add_new_role' );
function sgo_add_new_role( $default_capabilities ) {
  // Allow new user role to flush cache.
  $default_capabilities[] = 'delete_others_posts'; // For Editors.
  $default_capabilities[] = 'edit_published_posts'; // For Authors.

  return $default_capabilities;
}

Limit the number of URLs my website will preheat

We’ve also introduced an additional filter that enables you to manage the number of URLs your website will preheat effortlessly. By default, this value is set to 200, ensuring optimal performance.

Here’s an example of code that you can seamlessly incorporate into your functions.php file:

add_filter( 'sg_file_caching_preheat_url_limit', 'sgo_preheat_limit' );
function sgo_preheat_limit( $xml_urls ) {
  // Define custom limit for XML URL preheat.
  $xml_urls = 300;

  return $xml_urls;
}

Remember that when making modifications to the file-cache-related filters provided below, it’s essential to flush the cache. This action regenerates the sgo-config file and ensures the filters are successfully added.

Adding a cache bypass cookie

If you need to add a cache bypass cookie to the existing default ones, you can utilize the following filter:

add_filter( 'sgo_bypass_cookies', 'add_sgo_bypass_cookies');
function add_sgo_bypass_cookies( $bypass_cookies ) {
  // Add the cookies, that you need to bypass the cache.
  $bypass_cookies[] = 'cookie_name';
  $bypass_cookies[] = 'cookie_name_2';

  return $bypass_cookies;
}

If you want to skip the cache for a specific query parameter, you can use the following filter:

add_filter( 'sgo_bypass_query_params', 'add_sgo_bypass_query_params');
function add_sgo_bypass_query_params( $bypass_query_params ) {
  // Add custom query params, that will skip the cache.
  $bypass_query_params[] = 'query_param';
  $bypass_query_params[] = 'query_param2';

  return $bypass_query_params;
}

If you need to add a specific query parameter that will be ignored in the cache-creation and cache-spawn processes, you can do it using this filter:

add_filter( 'sgo_ignored_query_params', 'add_sgo_ignored_query_params');
function add_sgo_ignored_query_params( $ignored_query_params ) {
  // The query parameters which will be ignored.
  $ignored_query_params[] = 'query_param';
  $ignored_query_params[] = 'query_param2';

  return $ignored_query_params;
}

Exclude certain URLs from being cached

If you want to exclude certain URLs from your website’s caching, you can use the filter we have designed for that purpose. Make sure to surround the URL part with forward slashes. Wildcards can be used as well. Check out the below example:

add_filter( 'sgo_exclude_urls_from_cache', 'sgo_add_excluded_urls_from_cache');
function sgo_add_excluded_urls_from_cache( $excluded_urls ) {
  // The part of the URL which needs to be excluded from cache.
  $excluded_urls[] = '/excluded_url/';
  $excluded_urls[] = '/wildcard/exclude/*';

  return $excluded_urls;
}

Exclude specific tables from Heartbeat control

Tailor the optimization process by excluding specific tables from being optimized. You can fine-tune the optimization process to suit your specific needs by specifying the table name without the database prefix. Here’s an example of code that you can add to your functions.php file:

add_filter( 'sgo_db_optimization_exclude', 'sgo_db_optimization_exclude_table' );
function sgo_db_optimization_exclude_table( $excluded_tables ) {
  // Add tables that you need to exclude without the wpdb prefix.
  $excluded_tables[] = 'table_name';
  $excluded_tables[] = 'another_table_name';

  return $excluded_tables;
}

Customize maximum image width

You can also modify the maximum width setting to meet your specific needs. With this level of customization, you have the flexibility to optimize image sizes based on your unique requirements. Here’s an example code snippet that you can add to your functions.php file:

add_filter( 'sgo_set_max_image_width', 'max_image_width' );
function max_image_width( $max_allowed_width ) {
  // Add the value you want to adjust as max image width.
  $max_allowed_width = 1250;

  return $max_allowed_width;
}

Modify the default WebP quality setting

You can modify the default Webp quality setting using the filter we’ve designed for that purpose. The default setting is 80, and you can use values between 0 and 100:

add_filter( 'sgo_webp_quality', 'webp_quality' );
function webp_quality( $quality ) {
  // Add the value you want to adjust as Webp image quality.
  $quality = 100;

  return $quality;
}

You can modify the default Webp quality type setting using the filter we’ve designed for that purpose. The default quality type is lossy, which equals 0 if you want to set it to lossless – adjust the type to 1 as follows:

add_filter( 'sgo_webp_quality_type', 'reset_webp_quality_type' );
function reset_webp_quality_type( $quality_type ) {
  // Add the value you want to adjust as max image width.
  $quality_type = 1;

  return $quality_type;
}

Plugin Compatibility

In some cases, certain plugins may not trigger standard WordPress hooks or require cache purging. To address these situations, we have provided a public function that you can utilize in your code.

if (function_exists('sg_cachepress_purge_cache')) {
  sg_cachepress_purge_cache();
}

Preferably, you can pass an URL to the function to clear the cache just for it instead of purging the entire cache. For example:

if (function_exists('sg_cachepress_purge_cache')) {
  sg_cachepress_purge_cache('https://yoursite.com/pluginpage');
}

You can exclude styles from being combined and minified using the filters we’ve designed for that purpose. Here’s an example of the code you can add to your functions.php file:

add_filter( 'sgo_css_combine_exclude', 'css_combine_exclude' );
function css_combine_exclude( $exclude_list ) {
  // Add the style handle to exclude list.
  $exclude_list[] = 'style-handle';
  $exclude_list[] = 'style-handle-2';

  return $exclude_list;
}

add_filter( 'sgo_css_minify_exclude', 'css_minify_exclude' );
function css_minify_exclude( $exclude_list ) {
  // Add the style handle to exclude list.
  $exclude_list[] = 'style-handle';
  $exclude_list[] = 'style-handle-2';

  return $exclude_list;
}

You can exclude scripts from being minified using the filter we’ve designed for that purpose. Here’s an example of the code you can add to your functions.php file:

add_filter( 'sgo_js_minify_exclude', 'js_minify_exclude' );
function js_minify_exclude( $exclude_list ) {
  $exclude_list[] = 'script-handle';
  $exclude_list[] = 'script-handle-2';

  return $exclude_list;
}

Exclude scripts from being combined using the filter we’ve designed for that purpose. Here’s an example of the code, you can add to your functions.php file:

add_filter( 'sgo_javascript_combine_exclude', 'js_combine_exclude' );
function js_combine_exclude( $exclude_list ) {
  $exclude_list[] = 'script-handle';
  $exclude_list[] = 'script-handle-2';

  return $exclude_list;
}

or alternatively:

add_filter( 'sgo_javascript_combine_exclude_ids', 'js_sgo_javascript_combine_exclude_ids' );
function js_sgo_javascript_combine_exclude_ids( $exclude_list ) {
  $exclude_list[] = 'javascript-id1';
  $exclude_list[] = 'javascript-id2';

  return $exclude_list;
}

Or exclude an external script from being combined using the filter we’ve designed for that purpose. Here’s an example of the code you can add to your functions.php file:

add_filter( 'sgo_javascript_combine_excluded_external_paths', 'js_combine_exclude_external_script' );
function js_combine_exclude_external_script( $exclude_list ) {
  $exclude_list[] = 'script-host.com';
  $exclude_list[] = 'script-host-2.com';

  return $exclude_list;
}

You can exclude inline scripts from being combined using the filter we’ve designed for that purpose. Here’s a code snippet, you can add to your functions.php file:

add_filter( 'sgo_javascript_combine_excluded_inline_content', 'js_combine_exclude_inline_script' );
function js_combine_exclude_inline_script( $exclude_list ) {
  $exclude_list[] = 'first few symbols of inline content script';

  return $exclude_list;
}

You can exclude all inline scripts from being combined. Here’s an example code snippet you can add to your functions.php file:

add_filter( 'sgo_javascript_combine_exclude_all_inline', '__return_true' );

Additionally, you can exclude all inline scripts from being combined using the filter code below and adding it to your functions.php file.

add_filter( 'sgo_javascript_combine_exclude_all_inline_modules', '__return_true' );

Exclude script from being loaded asynchronously by adding the code shown below in your functions.php file.

add_filter( 'sgo_js_async_exclude', 'js_async_exclude' );
function js_async_exclude( $exclude_list ) {
  $exclude_list[] = 'script-handle';
  $exclude_list[] = 'script-handle-2';

  return $exclude_list;
}

Exclude a specific URL or a URL that contains a specific query param using the following filters:

add_filter( 'sgo_html_minify_exclude_params', 'html_minify_exclude_params' );
function html_minify_exclude_params( $exclude_params ) {
  // Add the query params that you want to exclude.
  $exclude_params[] = 'test';

  return $exclude_params;
}

add_filter( 'sgo_html_minify_exclude_urls', 'html_minify_exclude' );
function html_minify_exclude( $exclude_urls ) {
  // Add the url that you want to exclude.
  $exclude_urls[] = 'http://mydomain.com/page-slug';

  return $exclude_urls;
}

You can exclude static resources from the removal of their query strings using the filter we’ve designed for that purpose. Here’s an example of the code you can add to your functions.php file:

add_filter( 'sgo_rqs_exclude', 'sgo_rqs_exclude_scripts' );
function sgo_rqs_exclude_scripts( $exclude_list ) {
  $exclude_list[] = 'part-of-the-resource-path.js';
  return $exclude_list;
}

Also, you can exclude images from Lazy Load using the following filter:

add_filter( 'sgo_lazy_load_exclude_classes', 'exclude_images_with_specific_class' );
function exclude_images_with_specific_class( $classes ) {
  // Add the class name that you want to exclude from lazy load.
  $classes[] = 'test-class';

  return $classes;
}

Additionally, you can exclude specific post type from Lazy Load using the following filter:

add_filter( 'sgo_lazy_load_exclude_post_types', 'exclude_lazy_load_from_post_type' );
function exclude_lazy_load_from_post_type( $post_types ) {
  // Add the post type that you want to exclude from using lazy load.
  $post_types[] = 'post-type';

  return $post_types;
}

You can exclude a specific URL from Lazy Load using the following filter:

add_filter( 'sgo_lazy_load_exclude_urls', 'exclude_lazy_load_for_url' );
function exclude_lazy_load_for_url( $excluded_urls ) {
  // Add the url that you want to exclude from using lazy load.
  $excluded_urls[] = 'http://mydomain.com/page-slug';

  return $excluded_urls;
}

With these new filters, you can exclude specific assets from being Lazy Loaded. Keep in mind that using those filters can reduce performance in some cases.

You can use this filter to exclude specific images by adding their source URL:

add_filter( 'sgo_lazy_load_exclude_images', 'exclude_images_from_lazy_load' );
function exclude_images_from_lazy_load( $excluded_images ) {
  // Add the src url of the image that you want to exclude from using lazy load.
  $excluded_images[] = 'http://mydomain.com/wp-content/uploads/your-image.jpeg';

  return $excluded_images;
}

You can use this filter to exclude specific videos by adding their source URL:

add_filter( 'sgo_lazy_load_exclude_videos', 'exclude_videos_from_lazy_load );
function exclude_videos_from_lazy_load( $excluded_videos ) {
  // Add the src url of the video that you want to exclude from using lazy load.
  $excluded_videos[] = 'http://mydomain.com/wp-content/uploads/your-video.mp4';

  return $excluded_videos;
}

You can use this filter to exclude specific iframe by adding their source URL:

add_filter( 'sgo_lazy_load_exclude_iframe', 'exclude_iframe_from_lazy_load );
function exclude_iframe_from_lazy_load( $excluded_iframe ) {
  // Add the src url of the iframe that you want to exclude from using lazy load.
  $excluded_iframe[] = 'http://mydomain.com/wp-content/uploads/iframe-src.mp4';

  return $excluded_iframe;
}

Summary

This guide provided insights into the Speed Optimizer’s Custom Filters, which allow you to tailor your website’s performance to your specific needs. These filters enable you to control cache purge permissions, manage the number of preheated URLs, add cache bypass cookies, and exclude certain URLs from being cached. The guide also showed how to customize the maximum image width, modify the default WebP quality setting, and exclude specific tables from Heartbeat control.

We provided code examples for each filter, helping you implement these features easily. By leveraging these custom filters, you can significantly enhance your website’s performance, delivering a smoother and faster user experience.

Speed Optimizer FAQs

After enabling Combine CSS/JS option from the plugin, my website is not displaying as it should. What could be the reason?

If you find that combining certain CSS/JS files is breaking site functionality, you’ll need to exclude the relevant files from the combination. You can exclude certain elements from being combined using the exclude dropdown menu under Speed Optimizer > Frontend.

I am trying to exclude a stylesheet from being combined/minified by the plugin but I do not see it in the exclude list?

The drop-down list in your website’s code contains CSS Styles that have been implemented using standard coding procedures. CSS Styles that have been added without adhering to the recommended coding standards will not appear in the drop-down list. To include these styles, you need to utilize exclusion code filters. In this case, you must identify or be familiar with the handle of the style rather than directly referencing the style sheet name. The handles can be excluded through the functions.php file of your active template.

You should use the following filter to exclude styles from being combined:

add_filter( 'sgo_css_combine_exclude', 'css_combine_exclude' );
function css_combine_exclude( $exclude_list ) {
  // Add the style handle to exclude list.
  $exclude_list[] = 'style-handle';
  $exclude_list[] = 'style-handle-2';
  return $exclude_list;
}

You should use the following filter to exclude styles from being minified:

add_filter( 'sgo_css_minify_exclude', 'css_minify_exclude' );
function css_minify_exclude( $exclude_list ) {
  // Add the style handle to exclude list.
  $exclude_list[] = 'style-handle';
  $exclude_list[] = 'style-handle-2';
  return $exclude_list;
}

How can I exclude a plugin from being affected by combine and defer render-blocking JavaScript optimizations? Enabling these optimizations currently disrupts the functionality of the website.

You can exclude scripts using the exclude dropdown menus under Speed Optimizer > Frontend > Javascript. If some files are not visible in the list, they aren’t properly enqueued but are most likely hardcoded. The script handles can be excluded through the functions.php file of your active template. You can use the following code snippets to exclude handles from combination and render-blocking.

To exclude files from being combined, you should use:

add_filter( 'sgo_javascript_combine_exclude', 'js_combine_exclude' );
function js_combine_exclude( $exclude_list ) {
  $exclude_list[] = 'script-handle';
  $exclude_list[] = 'script-handle-2';
  return $exclude_list;
}

To exclude script/s from being loaded asynchronously, use the filter shown below, which you can add to your theme’s functions.php file:

add_filter( 'sgo_js_async_exclude', 'js_async_exclude' );
function js_async_exclude( $exclude_list ) {
  $exclude_list[] = 'script-handle';
  $exclude_list[] = 'script-handle-2';
  return $exclude_list;
}

My image compression/conversion is stuck, what can I do to resolve this?

You should check if your application can execute cron jobs successfully. Usually, this would mean that the native cron is disabled, and no real cron job is created under your control panel.

Tutorial Menu

Share This Article