How to Exclude Posts/Pages from Autoptimize

Faizan ShaikhGuides, WordPress Snippets5 Comments

Share this
  • 28
    Shares

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

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 Thrives 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 just 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.

About the Author
Faizan Shaikh

Faizan Shaikh

Faizan has been working on the Internet since 4 years. He has studied B.Sc. IT. He likes helping businesses grow on the Internet & likes sharing his knowledge.


Share this
  • 28
    Shares
  • 28
    Shares

5 Comments 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. Faizan Shaikh

      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

Leave a Reply

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