How to Exclude Posts/Pages from Autoptimize

Faizan ShaikhGuides, WordPress Snippets2 Comments

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 Autopimize 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.

2 Comments on “How to Exclude Posts/Pages from Autoptimize”

Leave a Reply

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