Buried deep into our plugin advanced settings, lies a special feature: Exclude patterns. As you can guess, it allows our users to specify which files or folders are omitted from the optimization.
Using this feature is not as intuitive as we wanted to be, that’s why we wrote this article. Read further to find a few examples of how to use this feature.
What can I exclude with this feature?
All files and folders that can be identified with the accepted patterns.
By “patterns:, we mean some rules that our plugin understands. For example, “name:flower.jpg” pattern will let ShortPixel know that all JPEG images that end in flower should not be compressed.
A pattern consists of a type:value pair. Type can be: name, path, size. Let’s see how you could use each of them.
For a Media Library item, the pattern will be matched against the name of the main image – not of its thumbnails – and will exclude the image together with all its thumbnails.
How to exclude files
Use name type pattern to exclude particular images.
For example, if you wish exclude logo.jpg from optimization, then you will add this pattern in the Exclude patterns field:
A small change to the pattern will lead to a broader selection:
will select for exclusion all images – PNG/JPEG/GIF – that include the logo word in their name: nicelogo.jpg, alllogos.png, logo.gif and so on.
As you can see, exclude patterns sounds complicated, but it is really not.
Remember: For a “name” pattern, only the filename will be matched.
How to exclude folders
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 size pattern
Use size to exclude images bigger than a certain size.
The format for the “size” exclude is: minWidth-maxWidthxminHeight-maxHeight, for example size:1000-1100×2000-2200. You can also specify a precise size, as 1000×2000.
For the “size” type, which applies only to Media Library images, the main images (not thumbnails) that have the size in the specified range will be excluded.
The power of 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 pinterest in the name and are from 2014: /\/2014\/.*pinterest[^\/]*/
Other rules to consider
These rules allow you to build more patterns.
1. You can add multiple patterns separated by a comma.
2. A file will be excluded if it matches any of the patterns.
ShortPixel will not compress example.jpg and it also let all images from social folders and included subfolders untouched.
3. For these you can also use regular expressions accepted by preg_match, but without “,” or “:”. A pattern will be considered a regex if it starts with a “/” and is valid.
Regular Expression resources: