This is a feature available in the Pro version of Asset CleanUp.

Remember: When you unload a plugin, you would not only unload all the CSS/JS that is loading from that plugin but everything else: its backend PHP code, HTML output printed via wp_head() or wp_footer() action hooks, cookies that are set), etc. It would be like the plugin is deactivated for the pages where it’s chosen to be unloaded.

The unload rules you can currently use include:

  • Unload site-wide (everywhere) (site-wide unload of an asset)
  • Unload it only for URLs with request URI matching a specified RegExes

The current load exception rules (if any of the unload rules are used) include:

  • Make an exception and always load it if the URL (its URI) is matched by specified RegExes
  • Always load the targeted plugin if the user is logged in

How does unloading or loading as an exception via a RegEx works?

You could use the power of regular expressions and if there’s a match in the request URI, then the targeted plugin will be unloaded. For example, you might want to unload the “Contact Form 7” plugin on all pages except the “Contact” page. If the URL to the contact page is something like https://yourdomainnamehere.com/contact-us/ you can choose the option “Unload site-wide (everywhere) & add an exception” (which will unload the plugin everywhere in the front-end view, like it’s deactivated), then tick “Make an exception and always load it if the URL (its URI) is matched by a RegEx:” checkbox and add the RegEx to load (as an exception) on the /contact-us/ page (based on the example that was just mentioned):

Note: Asset CleanUp is not checking for the domain name, but the request URI (for the techies it’s the $_SERVER[‘REQUEST_URI’] value in PHP). For example, if your URL is like https://mydomainnamehere.com/category-title/cat-name-slug-here/ the request URI would be /category-title/cat-name-slug-here/. As you noticed, the protocol and the hostname aren’t checked. This is keeping things simple and relative and it’s useful especially if you’re moving data (via the “Import & Export” feature in “Tools”) from a staging environment to the live website.

Other Regular Expression examples:

  • #/parent-page-title/(.*)/# – matches URLs like /parent-page-title/child-page-one-slug/, /parent-page-title/child-page-two-slug/, /parent-page-title/child-page-another-slug/, etc. – useful to match all child pages from a parent page
  • #/books/top-10-# – matches URLs like /books/top-10-sci-fi/, /books/top-10-action-and-adventure/, /books/top-10-drama/ etc.

If you have pages such as https://yourdomainnamehere.com/course/how-to-lose-weight/ and https://yourdomainnamehere.com/register-for-a-course/ and you want to be sure that only pages that start with /course/ are matched, you can specify that you want the request URI to start with /course/, thus /register-for-a-course/ will not be matched here’s how you can do it:
#^/course/# – the ^ will clearly instruct the script that the request URI should start with /course/

Case Scenario #1

Unload a plugin only on the homepage
You have to choose the option “Unload it only for URLs with request URI matching this RegEx” and in the input use the following RegExes (depending on the UR):

  • #(^/$)|(^/\?)# (if your homepage is like https://yourwebsite.com/, the RegEx will match if the Request URI is equal with / or starts with /? because it has query strings)
  • #(^/my-wordpress-installation-dir/$)|(^/my-wordpress-installation-dir/\?)# (if your homepage is like https://yourwebsite.com/my-wordpress-installation-dir/, the RegEx will match if the Request URI is equal with /my-wordpress-installation-dir/ or starts with /my-wordpress-installation-dir/? because it has query strings)

Case Scenario #2

Unload a plugin on all WooCommerce category and product pages
Im this example, we’ll use “product” as the parent slug of a WooCommerce product page. If you changed it for any reasons (e.g. you wanted to have it translated in other language), then replace “product” with the slug you’ve chosen using the examples below:

If you’re sure you’re using a plugin (e.g. generates a popup to subscribe to the email, or a plugin that generates contact forms, etc.) you know it will never need to be loaded on a product page) then you can use the option “Unload it only for URLs with request URI matching this RegEx” and use the following RegEx:
#/product/(.*?)/# (for the product pages)
This will match anything like https://yourwebsite.com/product/blue-jacket/, https://yourwebsite.com/product/nike-shoes/

#/product-category/(.*?)/# (for the category pages, assuming you’re using the default “product-category” slug)
This will match anything like https://yourwebsite.com/product-category/men-jackets/, https://yourwebsite.com/product-category/women-shoes/, https://yourwebsite.com/product-category/beauty-products/

In the following print screen, the mentioned RegEx rules are applied to “Contact Form 7”, which should never load on a WooCommerce category/product page.

Was this post helpful?