{"id":3231,"date":"2018-04-11T15:11:44","date_gmt":"2018-04-11T15:11:44","guid":{"rendered":"https:\/\/shortpixel.com/blog\/?p=3231"},"modified":"2022-06-13T15:39:35","modified_gmt":"2022-06-13T12:39:35","slug":"shortpixel-how-to-exclude-images-and-folders-from-optimization","status":"publish","type":"post","link":"https:\/\/shortpixel.com\/blog\/shortpixel-how-to-exclude-images-and-folders-from-optimization\/","title":{"rendered":"ShortPixel: How to exclude images and folders from optimization"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"684\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2018\/03\/adrian-williams-76616-unsplash-1024x684.jpg\" alt=\"\" class=\"wp-image-3232\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/adrian-williams-76616-unsplash-1024x684.jpg 1024w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/adrian-williams-76616-unsplash-300x200.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/adrian-williams-76616-unsplash-768x513.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/adrian-williams-76616-unsplash.jpg 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>Buried deep into our plugin advanced settings, lies a special feature: <em>Exclude patterns<\/em>. As you can guess, it allows our users to specify which files or folders are omitted from the optimization.<\/p>\n\n\n\n<p>Using this feature is not as intuitive as we wanted to be, that&#8217;s why we wrote this article. Read further to find a few examples of how to use this feature.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">What can I exclude with this feature?<\/h2>\n\n\n\n<p>All files and folders that can be identified with the accepted patterns.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"579\" height=\"38\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2018\/03\/nameflower.png\" alt=\"\" class=\"wp-image-3250\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/nameflower.png 579w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2018\/03\/nameflower-300x20.png 300w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/figure><\/div>\n\n\n\n<p>By patterns, we mean some rules that our plugin understands. For example, the <code>name:flower.jpg<\/code> pattern will let ShortPixel know that all JPEG images that end in &#8220;flower&#8221; should not be compressed.<\/p>\n\n\n\n<p>A pattern consists of a <code>type:value<\/code> pair. Type can be: <code>name<\/code>, <code>path<\/code>, <code>size<\/code>, <code>regex-path<\/code> or <code>regex-name<\/code>. Let&#8217;s see how you could use each of them.<\/p>\n\n\n\n<p>For a Media Library item, the pattern will be matched against the name of the <strong>main<\/strong> image &#8211; not of its thumbnails &#8211; and will exclude the image together with all its thumbnails.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to exclude files<\/h3>\n\n\n\n<p>Use the <code>name<\/code><em>&nbsp;<\/em>type pattern to exclude particular images.<\/p>\n\n\n\n<p>For example, if you wish to exclude &#8220;logo.jpg&#8221;<em> <\/em>from optimization, then you will add this pattern in the Exclude patterns<em> <\/em>field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name:logo.jpg<\/code><\/pre>\n\n\n\n<p>A small change to the pattern will lead to a broader selection. The following will select for exclusion all images &#8211; PNG\/JPEG\/GIF &#8211; that include the &#8220;logo&#8221; word in their name: &#8220;nicelogo.jpg&#8221;, &#8220;alllogos.png&#8221;, &#8220;logo.gif&#8221; and so on:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name:logo<\/code><\/pre>\n\n\n\n<p>As you can see, the feature Exclude patterns sounds complicated, but it is really not.<\/p>\n\n\n\n<p><strong>Remember<\/strong>: For a <code>name<\/code> pattern, only the filename will be matched.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to exclude folders<\/h3>\n\n\n\n<p>Use the <code>path<\/code> type pattern to exclude folders\/subfolders.<\/p>\n\n\n\n<p>All the path will be matched (useful for excluding certain subdirectories altogether). For these, you can also use regular expressions accepted by <code>preg_match<\/code>, but without &#8220;,&#8221; or &#8220;:&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to use the size pattern<\/h3>\n\n\n\n<p>Use <code>size<\/code> to exclude images bigger than a certain size.<\/p>\n\n\n\n<p>The format for the <code>size<\/code> exclude is: <code>minWidth-maxWidthxminHeight-maxHeight<\/code>. For example, <code>size:1000-1100x2000-2200<\/code>. You can also specify a precise size, as 1000&#215;2000.<\/p>\n\n\n\n<p>For the <code>size<\/code> type, which applies only to Media Library images, the main images (not thumbnails) that have the size in the specified range will be excluded.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The power of regular expressions<\/h3>\n\n\n\n<p>The regular expressions can help you set different exclude patterns. These are to be used with the types <code>regex-name<\/code> and <code>regex-path<\/code>. A regular expression must be contained between slashes (<code>\/<\/code>) and special characters should be escaped by adding <code>\\<\/code> in front of them. Here are just a couple of examples that will show their power:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All images from 2016: <code>regex-path:\/\\\/2016\\\/\/<\/code><\/li><li>All PNG images that have a numeric prefix: <code>regex-name:\/[0-9]+[^\\\/]*\\.(PNG|png)\/<\/code><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Regular Expression resources<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expression-language-quick-reference\" data-type=\"URL\" data-id=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expression-language-quick-reference\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft docs<\/a><\/li><li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/li><li><a href=\"http:\/\/www.rexegg.com\/regex-quickstart.html\" data-type=\"URL\" data-id=\"http:\/\/www.rexegg.com\/regex-quickstart.html\" target=\"_blank\" rel=\"noreferrer noopener\">Regex Cheat Sheet<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Other rules to consider<\/h2>\n\n\n\n<p>These rules allow you to build more&nbsp;patterns.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You can add multiple patterns separated by a comma. For example: <code>name:example.jpg,path:themeimages\/social<\/code><\/li><li>A file will be excluded if it matches any of the patterns.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/photos\/AU07BMLW1NA?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener\">Adrian Williams<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener\">Unsplash<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s why we wrote this article. Read further to find a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3231","post","type-post","status-publish","format-standard","hentry","category-world-of-wordpress"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/3231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/comments?post=3231"}],"version-history":[{"count":19,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/3231\/revisions"}],"predecessor-version":[{"id":6689,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/3231\/revisions\/6689"}],"wp:attachment":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media?parent=3231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/categories?post=3231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/tags?post=3231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}