How to exclude images optimization in WordPress plugin

Exclude images optimization in WordPress plugin
Image from Pixabay

If you tried and explored more CheetahO WordPress plugin, perhaps noticed that from version 1.5 we have a new feature: Exclude images using patterns. This new feature allows our users to specify which files or folders are omitted from the image optimization process.

For some plugin users, this feature can look not understanding how to use it. So we will try to explain how it works in this article and show a few examples of how it works. 

What can this feature exclude?

This feature can exclude images from being optimized, based on patterns. A file will be not optimized if it matches any of the patterns.
Few patterns can be separated by a comma. A pattern consists of a type:value pair. Supported types are: name, path
For a “name” patter only the file name will be matched, but for the “path” all the path will be matched.
If a pattern will start with “/” and is valid it will be considered as a regex. You can also use regular expressions accepted by preg_match, but without “,” or “:”.

Exclude image configuration

How to exclude image by filename?

This can be done by using the name type pattern.

For example, you want to exclude a file with the name demo.jpg from optimization. What you need to do is to add this pattern in the Exclude images using patterns field like this:

name:demo.jpg

Let’s say you want to exclude all images – PNG/JPEG/GIF.  A small change to the pattern will do a trick:

name:demo

How to exclude a specific folder?

To do this use path type pattern to exclude folders/subfolders

All the path will be matched (useful for excluding certain subdirectories altogether). For these, you can also use regular expressions accepted by preg_match, but without “,” or “:”.

How to use regular expressions?

The regular expressions can help you set different exclude patterns. Here are just a few examples that will show their power:

All images from 2016: path:/\/2016\//

All PNG images that have a numeric prefix: path:/[0-9]+[^\/]*\.(PNG|png)/

All images that contain cheetaho in the name and are from 2020: /\/2020\/.*cheetaho[^\/]*/

Some other tips for using this feature

  • Images optimization in the WordPress plugin will stop if the rule will match any of the patterns.
  • It is possible to use multiple patterns and can be separated by a comma.
  • It’s possible to use regular expressions accepted by preg_match. A pattern will be used as a regex if it starts with a “/” and is valid.

We are suggesting to read more about regex here.