ShortPixel: How to exclude images and folders from optimization
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: name
, path
, size
, regex-path
or regex-name
. 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 the 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:
name:logo.jpg
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:
name:logo
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
Use the 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
Use 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.
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. These are to be used with the types regex-name
and regex-path
. A regular expression must be contained between slashes (/
) and special characters should be escaped by adding \
in front of them. Here are just a couple of examples that will show their power:
- All images from 2016:
regex-path:/\/2016\//
- All PNG images that have a numeric prefix:
regex-name:/[0-9]+[^\/]*\.(PNG|png)/
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:
name:example.jpg,path:themeimages/social
- A file will be excluded if it matches any of the patterns.
Photo by Adrian Williams on Unsplash
I want to use the “size” option to optimize images that are not in the Media Library. From your documentation, it seems that “size” will only work for the Media Library. Is there a work around? I only want to send images to your server (and use credits) for images above a certain size and these images are not in the Media Library. – Thanks