Exclude old images from optimization and automatically optimize the new images

Imagine a scenario where your website has already uploaded and optimized many images. Now you decide to use ShortPixel Image Optimizer to compress only your new images that you upload from now on. No problem, you say, you can just enable the Optimize media on upload option and that's it. But there's a catch: once you run the bulk optimization on Media > Bulk ShortPixel, the plugin will optimize all your images, even the old ones. This can be problematic, as you might end up with over-optimized images and you'll probably spend too many unnecessary credits.

The solution lies in the Exclude patterns setting.

Attention! This solution only works if you have always enabled the setting "Organize my uploads into month- and year-based folders" in the Media settings. This way, the URLs of your images will look like this: https://mydomain.com/wp-content/uploads/2018/08/image.jpg

Exclusion by year

Suppose you want to exclude all images uploaded before 2022 from optimization. Go to the Exclude patterns field and write the following:

regex-path:/\/(19[0-9][0-9]|20[01][0-9]|202[01])\//

Explanation

You need to understand how these regular expressions work if you want to further customize your filter. Therefore, let us explain each part of the previous example.

The first and the last / are always required.

Each of the \/ matches the slash before and after the year in the URL: ...uploads/2018/08...

The parenthesis are used as a capture group for the next part, which is 19[0-9][0-9]|20[01][0-9]|202[01].

  • The expression has three parts separated by |. The first is 19[0-9][0-9], which means that it matches any 4-digit number that starts with 19.
  • The second is 20[01][0-9], which means it matches any number from 2000 to 2019: 20 always stays the same, the third digit can be 0 or 1, and the fourth can be anything.
  • The third part is 202[01]. This matches 2020 or 2021; the only change is the last digit.

Other examples

  • Exclude images uploaded before 2018: regex-path:/\/(19[0-9][0-9]|200[0-9]|201[0-7])\//
  • Exclude images uploaded before 2007: regex-path:/\/(19[0-9][0-9]|200[0-6])\//
  • Exclude images uploaded before 1999: regex-path:/\/(19[0-8][0-9]|199[0-8])\//

Exclusion by year and month

Suppose you want to exclude from optimization all images uploaded before May 2022. Go to the Exclude patterns field and write the following:

regex-path:/\/((19[0-9][0-9]|20[01][0-9]|202[01])|2022\/0[1-4])\//

Explanation

Here we use the parenthesis to capture two groups: either all months from all years from 1900 to 2021 (19[0-9][0-9]|20[01][0-9]|202[01]) or January to April 2022 (2022\/0[1-4]). That's effectively excluding anything before May 2022.

Other examples

  • Exclude images uploaded before November 2015: regex-path:/\/((19[0-9][0-9]|200[0-9]|201[0-4])|2015\/0[1-9]|2015\/10)\//
  • Exclude images uploaded before March 2008: regex-path:/\/((19[0-9][0-9]|200[0-7])|2008\/0[12])\//
  • Exclude images uploaded before August 1999: regex-path:/\/(199[0-8]|1999\/0[1-7])\//

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us