How to Exclude Posts/Pages from Autoptimize

Autoptimize is one of the best WordPress plugins for minifying & caching scripts & styles. However, some plugins may not work correctly with its minification feature. One such example is Thrives Quiz Builder, which I had issues with while loading on some browsers. Hence you may want to disable Autoptimize on some pages. This guide will teach you how to exclude Autoptimize on some pages.

How to exclude Posts/Pages from Autoptimize

The first method(Using functions.php)

There is a small snippet you need to add to your functions.php to do so. Make sure you are using a child theme; else, this will get overridden after updating your theme. I had to disable Autoptimize on pages, where a quiz(created by Thrive Quiz Builder) was there because it wasn’t playing well with Internet Explorer & Chrome on some devices. So, in my case, all the URL’s where there was a quiz contained the word “test”. So I did not want Autoptimize to work on any pages where the URL included the word “test”. Don’t forget to replace the word “test” with your desired word, which occurs on pages where you want to disable Autoptimize. Here is the code you need to exclude Autoptimize on some pages. Add this to your functions.php

// Disable autoptimize on pages with the word "test" in the URL
add_filter('autoptimize_filter_noptimize','my_ao_noptimize',10,0);
function my_ao_noptimize() {
if (strpos($_SERVER['REQUEST_URI'],'test')!==false) {
return true;
} else {
return false;
}
}

If you’d like to add multiple URL terms, then you can add the following code:

// Disable autoptimize on all pages with the words "term1" or "term2" in the URL
add_filter('autoptimize_filter_noptimize','my_ao_noptimize',10,0);
function my_ao_noptimize() {if ((strpos($_SERVER['REQUEST_URI'],'term1')!==false) || if (strpos($_SERVER['REQUEST_URI'],'term2')!==false))return true;
} else {
return false;
}
}

Note: Do not forget to replace the word “term1” & “term2” with your desired terms which occur in the URL.

The second method (Simpler)

Another method is to add the code below anywhere inside your post content. It needs to be added in the text tab and not in the visual tab of the content editor, as shown in the image below. The credit for this method goes to the original plugin developer, Frank Goossens, who has written it on his blog here.

<!-- <xsl:stylesheet -->

Which method should you use?

It depends. If you want to disable Autoptimize on just one or two pages, then the second method is easier to implement. If you’re going to disable Autoptimize on many different pages/posts, then I would recommend the first method as that would be easier to maintain in the long run.

8 thoughts on “How to Exclude Posts/Pages from Autoptimize”

  1. Hi, I want to add multiple urls, and the code that you gave us, it says an error on the code.

    the fist one, for single url works fine.

    How can I add more than 2 urls??

    1. Hi,
      There was a typo in the second code. I have updated the post. Please add the code in the following format.
      // Disable autoptimize on all pages with the words "term1" or "term2" in the URL
      add_filter('autoptimize_filter_noptimize','my_ao_noptimize',10,0);
      function my_ao_noptimize() {if ((strpos($_SERVER['REQUEST_URI'],'term1')!==false) || if (strpos($_SERVER['REQUEST_URI'],'term2')!==false))return true;
      } else {
      return false;
      }
      }

  2. HI,

    In the next line, my editor says there is a syntax error, unexpected T_IF:

    function my_ao_noptimize() {if ((strpos($_SERVER[‘REQUEST_URI’],’term1′)!==false) || if (strpos($_SERVER[‘REQUEST_URI’],’term2′)!==false))return true;

    How can I add more url´s???

    thanks

  3. So the code for the multi-term doesn’t work in that format anymore, here is the updated code I put together, hope it helps!

    // Disable autoptimize on all pages with the words “term1” or “term2” in the URL
    add_filter(‘autoptimize_filter_noptimize’,’my_ao_noptimize’,10,0);
    function my_ao_noptimize() {
    if (strpos($_SERVER[‘REQUEST_URI’],’term 2′)!==false) {
    return true;
    }
    elseif (strpos($_SERVER[‘REQUEST_URI’],’term 1′)!==false) {
    return true;
    }
    else {
    return false;
    }
    }

  4. My issue is plugin shortcode specific. Anyway you can think of to have Autoptimizer not load on any page or post that has a specific shortcode rather than work in it’s url? Thanks in advance for sharing your insight and knowledge.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get my free audit!

  • This field is for validation purposes and should be left unchanged.