[wp-hackers] Restrict plugin load to certain pages?

John Blackbourn johnbillion+wp at gmail.com
Mon Jul 1 10:40:49 UTC 2013


On 1 July 2013 11:30, John Blackbourn <johnbillion+wp at gmail.com> wrote:
> On 1 July 2013 10:02, David Anderson <david at wordshell.net> wrote:
>> Looking at the WP core code, my only option to achieve this seems to be to
>> be (via an mu-plugin, since a plugin would present a Catch-22) to hook
>> get_option, detect the option 'active_plugins', and remove the offending
>> plugin depending on the contents of _SERVER['REQUEST_URI'] and is_admin() (I
>> don't want to disable anything admin-side).
>
> You're approaching this from the wrong angle. Rather than not loading
> the plugin at all when it's not necessary, you should only load the
> required parts of your plugin when necessary. If your mu-plugin is
> able to detect the correct condition to determine when your forum
> plugin should or should not be loaded, then it can instead be done
> within the forum plugin itself.
>
> If your plugin is split up into separate files effectively then you
> should only load minimal code on every page load, detect if the rest
> of the plugin needs to be loaded (eg. by inspecting the URL as you
> state) and then loading its other files when necessary. There's no
> need for a separate plugin, and certainly no need for a nasty hack
> like filtering the value of the active plugins option.
>
> This can be as simple as (pseudo-code):
>
> if ( url_contains( 'forum' ) )
>     include 'forum-code.php';
>
> Lots of plugins use similar code for only loading files in the admin
> are. It's pretty simple stuff.
>
> if ( is_admin() )
>     include 'admin-code.php';
>
> Additionally, if your plugin makes good use of object oriented
> programming and is split into multiple classes then you can use
> autoloading.
>
> John

I've just realised you're probably talking about a third party plugin,
in which case my suggestion is mostly moot. Filtering the value of the
active plugins option is a pretty yucky hack, but I don't have any
other suggestions.

Bear in mind that disabling the plugin completely on pages other than
at "/forum" means you'll potentially lose out on related
functionality, such as sidebar widgets that display the latest forum
posts, integration with other parts of your site, etc etc. You'll also
need to ensure the plugin loads when WP-Cron fires, in case it's using
cron events.


More information about the wp-hackers mailing list