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_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_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 for pages only.

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

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

    return $timeout
});

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_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;
 });
Was this article helpful?

Related Articles