Learn how to disable plugins except for specified urls. Inspired by Plugin Organizer plugin, I disabled CF7 for all but the contact page.
A client contacted me last week reporting that their site was very slow to load. I went to the site and confirmed that it wasn’t just very slow, it was very, very, very slow – it took about 12 seconds to load!! After the first page was loaded it was noticeably quicker, but still quite sluggish.
The site is on shared hosting and the theme is a child of Twenty Twelve. I added the site to my Uptime Robot dashboard and it reported a time for first response of 11 seconds. I submitted a support ticket to the hosting company. They replied overnight to saying that there was nothing up with server.
The following day the load time had reduced considerably, though still about 4 seconds. I suspect that there was a temporary issue on the server e.g. another site gone wild.
I installed P3 (Plugin Performance Profiler) and it reported that Polylang was responsible for 65% of the page load time, followed by Contact Form 7 at 21%. Polylang could not be deactivated as it is required to present the site in three languages (English, French and German). Contact Form 7 is only used on three pages so the site would be okay if it was deactivated except for those pages.
I searched for suggestions to conditionally disable CF7 but the solutions all involved changing the pages that the CF7 assets (CSS and JS files) were loaded. I tried this but it made only a negligible difference – I was going to have to disable the entire plugin!
I found the Plugin Organizer plugin and tried it out. Despite a slightly strange interface (mostly a styling thing), I was able to globally disable the CF7 plugin and selectively enable on the pages with the CF7 forms. The site load time was further reduced.
While I wasn’t concerned that I added a plugin to disable another, I felt that I should be able to write code to do the same thing.
Plugin Organizer copies a file (lib/PluginOrganizerMU.class.php) into the wp-content/mu-plugins directory. I read through the code and saw its approach – filter the list of active plugins listed in the ‘active_plugins‘ option.
I did some debugging experiments with the ‘option_active_plugins‘ filter, sending data to the debug.log file. I saw that the CF7 plugin was listed as ‘contact-form-7/wp-contact-form-7.php‘ in the option.
My next decision was to decide whether to examine the active plugins first or the current url. I felt that examining the plugins would make for an easier data structure: plugin => array of urls.
The code goes through the list of active plugins. If a plugin is in the $disable_plugins list, it checks whether the requested url is in that plugin’s exclusion list. If not, it adds the plugin to a list of plugins to disable.
If any plugins need to be disabled it uses array_diff() to remove them from the list of active plugins (Plugin Organizer used array_diff – I’m constantly learning new array manipulation functions).
It is very simple code and the site’s load time is now about 600ms (according to P3), and about 1300ms according to Uptime Robot.
Note: This plugin must be installed in the wp-content/mu-plugins directory. It would run too late if activated like a regular plugin. The code is generic so you can add different plugins and urls.