Use code PERFMATTERS for an extra 10% off!
  1. Home
  2. Docs
  3. General
  4. Filters to further customize Perfmatters

Filters to further customize Perfmatters

Below is a list of all the filters available in Perfmatters. These allow you to further customize the plugin. If you need an easy way to add a filter, we recommend the free Code Snippets plugin.

perfmatters_cache_path

The perfmatters_cache_path filter allows you to change the default cache path (cache) to a custom directory inside wp-content.

add_filter('perfmatters_cache_path', function($path) {
    return 'custom/folder';
});

perfmatters_cdn

The perfmatters_cdn filter allows you to manipulate the CDN rewrite feature in Perfmatters. Below is an example of how to turn off the CDN rewrite selectively for pages only.

add_filter('perfmatters_cdn', function($cdn) {
  if(is_singular('page')) {
    return false;
  }
  return $cdn;
});

perfmatters_defer_js filter

The perfmatters_defer_js filter allows you to manipulate JS deferral. Below is an example of how to turn off defer on pages only.

add_filter('perfmatters_defer_js', function($defer) {
      if(is_singular('page')) {
        return false;
    }
    return $defer;
});

perfmatters_delayed_scripts

The perfmatters_delayed_scripts filter allows you to manipulate your delayed scripts. For example, you could exclude a specific script from being delayed on a desktop device.

add_filter('perfmatters_delayed_scripts', function($scripts) {
     if(($key = array_search('name_of_your_script', $scripts)) !== false) {
         if(!wp_is_mobile()) {
             unset($scripts[$key]);
         }
     }
     return $scripts;
 });

perfmatters_delay_js

The perfmatters_delay_js filter is for turning your delayed scripts on or off entirely. For example, you could turn off delay only on pages.

add_filter('perfmatters_delay_js', function($delay) {
      if(is_singular('page')) {
        return false;
    }
    return $delay;
});

perfmatters_delay_js_behavior

The perfmatters_delay_js_behavior filter allows you to manipulate the Delay JS behavior. For example, you could force Delay All JS behavior on single posts.

add_filter('perfmatters_delay_js_behavior', function($behavior) {
  
  if(is_singular('post')) {
	$behavior = 'all';
  }

  return $behavior;
});

perfmatters_delay_js_timeout

The perfmatters_delay_js_timeout filter allows you to manipulate the JS delay timeout feature. For example, you could change the delay timeout to 7 seconds instead of the automatic 10 seconds. However, make sure not to set the timeout value too short, otherwise the JS delay feature won’t work properly. Remember, 99% of the time everything will fire on user interaction regardless.

add_filter('perfmatters_delay_js_timeout', function($timeout) {
    return '7';
});

Here is another example where you could change the delay timeout to 7 seconds for pages only.

add_filter('perfmatters_delay_js_timeout', function($timeout) {

    if(is_singular('page')) {
        $timeout = '7';
    }

    return $timeout;
});

perfmatters_disable_woocommerce_scripts

The perfmatters_disable_woocommerce_scripts filter gives you more control over the Disable WooCommerce Scripts feature. For example, you could exclude a specific page from the script disables.

add_filter('perfmatters_disable_woocommerce_scripts', function($boolean) {
	if(is_page(123)) {
		return false;
	}
	return $boolean;
});

perfmatters_fade_in_speed

The perfmatters_fade_in_speed filter allows you to adjust the fade in transition speed in ms (default is 500). For example, you could change the speed to 1000 ms.

add_filter('perfmatters_fade_in_speed', function($speed) {
  return 1000; //speed in ms
});

perfmatters_get_current_ID

In some cases, you may want to add some additional logic of your own depending on the configuration of your site to ensure that the Script Manager is grabbing the correct ID for all of your posts. The perfmatters_get_current_ID filter allows you to modify and return a different value based on whatever you want.

function perfmatters_filter_current_ID($currentID) {

     $currentID = 123;

     return $currentID;
}
add_filter('perfmatters_get_current_ID', 'perfmatters_filter_current_ID');

perfmatters_lazyload

The perfmatters_lazyload filter allows you to manipulate the lazy loading feature in Perfmatters. Below is an example of how to disable all lazy loading specifically on pages only.

add_filter('perfmatters_lazyload', function($lazyload) {
    if(is_singular('page')) {
      return false;
    }
    return $lazyload;
});

perfmatters_lazyload_exclude_attributes

The perfmatters_lazyload_exclude_attributes filter helps you exclude images from lazy loading that might be harder to access and manipulate directly. It lets you add any attributes or portions of attributes that are specific to the image in question.

In the example below, we are targeting any images that contain either the title='Perfmatters' attribute or the partial class=’size-full attribute. This is simply a string match, so any portion of any attribute should work, it just needs to be an exact match for the image or images you are wanting to exclude.

function perfmatters_lazyload_exclude_attributes($attributes) {
     $attributes[] = "title='Perfmatters'";
     $attributes[] = "class='size-full";

     return $attributes;
}
add_filter('perfmatters_lazyload_excluded_attributes', 'perfmatters_lazyload_exclude_attributes');

Here is an example of an exclusion based on an image file name match.

function perfmatters_lazyload_exclude_attributes($attributes) {
     $attributes[] = 'src="https://domain.com/image.png"';

     return $attributes;
}
add_filter('perfmatters_lazyload_excluded_attributes', 'perfmatters_lazyload_exclude_attributes');

perfmatters_lazyload_force_attributes

The perfmatters_lazyload_force_attributes filter can be used the same as in the excluded attributes example above, but when an element contains one of the included attributes, our lazy loader will skip checking that item for exclusions and continue attempting to lazy load that element.

function perfmatters_lazyload_force_attributes($attributes) {
     $attributes[] = "title='Perfmatters'";
     $attributes[] = "class='size-full";

     return $attributes;
}
add_filter('perfmatters_lazyload_forced_attributes', 'perfmatters_lazyload_force_attributes');

perfmatters_lazyload_youtube_thumbnail_resolution

The perfmatters_lazyload_youtube_thumbnail_resolution filter allows you to change the quality of the thumbnail for YouTube. By default, a low-resolution image is pulled from YouTube, but you can change it to maxresdefault to pull the highest quality if available.

add_filter('perfmatters_lazyload_youtube_thumbnail_resolution', function($resolution) {
return 'maxresdefault';
});

You can also use the wp_is_mobile function to show low-resolution images on mobile devices while showing high-resolution thumbnails on a desktop.

add_filter('perfmatters_lazyload_youtube_thumbnail_resolution', function($resolution) {
if(!wp_is_mobile()) {
$resolution = 'maxresdefault';
}
return $resolution;
});

perfmatters_lazyload_threshold

The perfmatters_lazyload_threshold filter allows you to change the viewport threshold for lazy loading. It accepts px or %. You can also change this property in the lazy loading section in the Perfmatters plugin settings.

add_filter('perfmatters_lazyload_threshold', function($threshold) {
 return '500px';
});

perfmatters_page_builders

The perfmatters_page_builders filter gives you more control over which query string parameters get excluded from optimizations. For example, you could add your own unique page builder parameter.

add_filter('perfmatters_page_builders', function($page_builders) {
  $page_builders['custom-builder'] = true;
  return $page_builders;
});

perfmatters_preloads

The perfmatters_preloads filter allows you to manipulate your preloads. For example, you could remove a specific preload from printing.

add_filter('perfmatters_preloads', function($preloads) {
   foreach($preloads as $key => $preload) {    
     if($preload['url'] == 'https://example.com/style.css') {
       //custom code
       unset($preloads[$key]);
     }
   }
   return $preloads;
 });

perfmatters_remove_unused_css

You can use the perfmatters_remove_unused_css filter to change where unused CSS is removed on your site. Below is an example of turning unused CSS off only on pages.

add_filter('perfmatters_remove_unused_css', function($boolean) {
	if(is_page()) {
	  return false;
	}
  	return $boolean;
});

perfmatters_rucss_excluded_selectors

The perfmatters_rucss_excluded_selectors filter allows you to pass custom CSS selectors to the Excluded Selectors option that will be used when determining which elements can be safely removed when generating Used CSS.

add_filter('perfmatters_rucss_excluded_selectors', function($selectors) {
	$selectors[] = '.wp-block-column';
  	return $selectors;
});

perfmatters_script_manager_locale

The perfmatters_script_manager_locale filter allows you to change which language is used for the Script Manager UI, regardless of the default setting in WordPress. For example, you could force the Script Manager to use English, even if the site language is different.

add_filter('perfmatters_script_manager_locale', function($locale) {
  return 'en_US';
});
Was this article helpful?

Related Articles