Update 25th of Nov, 2016

Recently I’ve restarted my technical blog under https://kamilgrzegorczyk.com where you can find my newest articles about the development and everything around. I will be posting new tutorials mostly there but I plan to cross-post them also on this website.

A bit of theory

In my last project I tried to emphasize sites’ performance optimization – so I sat down and tried to analyze the impact of various plugins. I was analyzing the output of debug bar and the number of queries, I have also checked the source of particular pages to see what files are being loaded by 3rd party plugins.

Then I came across Cart66 plugin – the plugin which we have used in various previous projects for e-commerce functionality  I never was a fan of how this plugin was developed – mainly because of lack of optimization, very slight number of hooks and very limited customization options for advanced devs (overwriting already defined styles is not good way to customize things). Nevertheless it was doing its job – our clients were selling just few items so they weren’t expecting any “magento-like” functionality.

But lets move to the topic – performance – Cart66 was firing even 50-60 queries on pages which were not store pages. How ridiculous is that? It was also embeding CSS and JS files on every WP page . That is why I have decided to limit how the plugin was loaded.

There are mainly 2 ways of disabling the plugins:

  • Disabling scripts and styles by using wp_dequeue_style() and wp_dequeue_script( ) functions.
  • Disabling the plugin totally by using ‘option_active_plugins’ hook.

First option works flawlessly if we want just to disable styles or scripts from particular pages (for example home page). Unfortunately, if we want to be sure that plugins functions are not fired, that the plugin is not making any unnecessary queries we have to disable it totally.

The code

Lets make few assumptions:

  • we know the sites’ structure (page slugs, URLs),
  • we are pretty sure of how pages are named and structured (in my case all store pages are children of page named ‘store’),
  • we know where the plugin is used on our page,
  • we know all downsides of disabling the plugin,
  • our usage case is very simple.

As ‘option_active_plugins’  is fired before any plugin is loaded we need to drop our code down into mu-plugins directory (http://codex.wordpress.org/Must_Use_Plugins). Please also remember that those plugins are run before the main query is initialized – that is why we don’t have any access to many functionalities, especially conditional tags which will always return false.

I know that the code is not something “super-extra-fancy”, but it does its job – it excludes Cart66 from being run on all other pages than “e-commerce” ones. I have shaved over 50 queries and “total query time” was cut down by over a half.

We all know that WordPress is a big, complicated environment with many relationships, files, functionalities so If you know any caveats of this approach please let me know in the comments 😉

 

Posted by Kamil Grzegorczyk Owner / Development

  • Thanks for the tip, I also have web clients using Cart66, no complaints so far, although I enjoy learning where to optimize with Cart66 usage.

  • Gary Martin

    Hi great article. This is definitely something for thought on wordpress development. We utilize this CMS for a lot of our clients and performance is always a thought for us. Of course this has its pros and cons and you just need to take caution on which plugins you are going to disable.

  • Nils

    This Plugin seems to do this too:

    http://wordpress.org/plugins/plugin-organizer

  • John Burns
    • Kamil Grzegorczyk

      Hello,

      Unfortunately the plugin which you have linked is not doing anything more that disabling styles / scripts which is not the idea of this blog post

  • Kampiamo DiKuesto

    hi, with this method is it possible to upgrade wp | parent theme | plugin without problem? in other word does the new update ovveride this solution?

  • nicolabavaro

    Nice article! Great!
    Nice for Web Performance Optimization! Thanks a lot!

  • Thanks much for this, Kamil – I used it to disable the Shareaholic sharing plugin on pages where I didn’t need it (it was loading everywhere even when it was only used on blog posts). Worked great.

    • Kamil Grzegorczyk

      Hello, I am glad that I could help!

  • Neku

    May you help me? I have a plugin that must run on multiple directories like “download” and “downloads”. How would the code look like for this case? I tried adding another “AND” but it did not work 🙁

    • Kamil Grzegorczyk

      I am very sorry but I somehow missed your comment. I hope that you have managed it by now 🙂

  • Nice one, I’ve just released a plugin on wordpress.org that unloads the .JS and .CSS from pages where you don’t need those files. It’s called “WP Asset Clean Up”: https://wordpress.org/plugins/wp-asset-clean-up/ – Perhaps it would help someone here.

    • Kamil Grzegorczyk

      Sorry for late reply.

      It is a great idea – I am doing similar things in my projects to always tune up the number of assets and disable them wherever possible.

  • Aron

    Is this still working on latest WP ?

    • Kamil Grzegorczyk

      Unfortunately I do not have any idea. I did not tried it since long time 😉

  • Aron

    I love this code man,
    Ive been trying to make it work so that it disables a plugin only on frontpage.. But no matter what I try Im unable to get it going. Any idea ?

    • Kamil Grzegorczyk

      You would have to parse the request url to match the url of your homepage.

      • Aron

        i tried that , doesnt work. Can you show me a working example ?

  • Hello,

    Thanks for your helpful post.

    I just spend few times to understand why my WordPress site doesn’t works with AMP Automattic official plugin https://wordpress.org/plugins/amp/ and the culprit was the plugin Custom Permalink : https://wordpress.org/plugins/custom-permalinks/

    Problem : Custom Permalink make a 301 redirection from /amp/ URL to the original post.

    Anyway, I try to disable it on “posts” with « Plugin Organizer » (I use Custom Permalink only on “pages”), but doesn’t works for me… Maybe the Custom Permalink plugin is loaded to early ?

    So with few searches I find your quick-tip, thanks !

    I was not familiar with wp-content/mu-plugins folder and for me the code was not 100%… effective. Then, I give you the way I solved the problem by disabling Custom Permalink on AMP pages.

    I just change the code with this :

    Drop it in a file wp-content/mu-plugins/load.php (make the mu-plugins folder to be able to you a must-use plugin name here « load.php »).

    It’s enough. Clear all your cache and enjoy AMP with Custom Permalink actived on pages.

    Hope it helps some people.

    Thanks again !

  • Pingback: Disabling a Plugin When a User Is Logged In | BeSuperfly.()

Like what you see?

Contact us today and get started!