Disable specified plugins except for certain pages - enhanced with caching.
In May last year I wrote code to disable plugins except for specified urls primarily to reduce the load that Contact Form 7 was placing on a site. Someone commented to recommend caching the list of permitted plugins. I have rewritten the code as a PHP class to do this.
Having come from a procedural programming background I rarely write OOP code though encapsulation has been very useful when I have done so. I used that here to cache the list of permitted plugins. For flexibility I also changed how the url is examined.
I changed from array_search() to compare the permitted urls with the current url to a strpos() check of each permitted one individually. This change allows the Contact Form 7 REST API url through – it uses a url with the contact form ID. I decided against a regular expression in the permitted urls as regexs are often hard to read and understand. The strpos() check is that the url starts with the permitted url.
I also moved the is_admin() check outside the filter function. Now the filter function is not added if in the admin area.
As before, the plugin must be put in the wp-content/mu-plugins directory as it must run before regular plugins are loaded.