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, the
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:
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
name type pattern to exclude particular images.
For example, if you wish to 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. The following 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, the feature Exclude patterns sounds complicated, but it is really not.
Remember: For a
name pattern, only the filename will be matched.
How to exclude folders
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 the size pattern
size to exclude images bigger than a certain size.
The format for the
size exclude is:
minWidth-maxWidthxminHeight-maxHeight. For example,
size:1000-1100x2000-2200. You can also specify a precise size, as 1000×2000.
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:
- All PNG images that have a numeric prefix:
- All images that contain “pinterest” in the filename and are from 2014:
Regular Expression resources
Other rules to consider
These rules allow you to build more patterns.
- You can add multiple patterns separated by a comma. For example:
- A file will be excluded if it matches any of the patterns.
- A pattern will be considered a regex if it starts with
/and is valid.