In a previous article, we considered how to selectively disable certain plugin functions, in order to enhance page speed. I.e., only run expensive plugin functions on page content that needs it.
Disabling plugin functions may help to save us some time and resources. However, the plugin files will still be loaded, and the initialization methods will still run.
Here, we we consider how to selectively load an entire plugin for a given set of pages. We would leave the plugin in an “unactivated” state, so that we do not spend extra time and resources on most of our pages. Then, we can dynamically load the plugin just for a chosen number of pages.
For example, the code below selectively loads my Shiba Background plugin for only certain specified pages.
if (!class_exists("My_Plugin")) : class My_Plugin { function __construct() { if (is_admin()) { include(WP_PLUGIN_DIR . '/shiba-custom-background/shiba-custom-background.php'); } elseif ( $this->check_uri('/wordpress-theme/example-thematic-background') || $this->check_uri('/wordpress-theme/twenty-ten-unleashed') || $this->check_uri('/wordpress-theme/twenty-ten-glamour') || $this->check_uri('/wordpress-theme/twenty-ten-car-girl-theme') || $this->check_uri('/wordpress-theme/thematic-widgets-plugin') ) include(WP_PLUGIN_DIR . '/shiba-custom-background/shiba-custom-background.php'); } function check_uri($url) { if (strpos($_SERVER['REQUEST_URI'], $url) === 0) return TRUE; else return FALSE; } } // end class endif; global $my_plugin; if (class_exists("My_Plugin") && !$my_plugin) { $my_plugin = new My_Plugin(); }
We can include the code in an existing plugin, or create a new plugin.
Line 5 – Note that we check for pages in the constructor function so that if need be, we load in our “selective plugin” during the usual plugin load phase. This will help ensure that initialization functions and other plugin actions get executed normally.
If we wait until later to load the plugin, we may miss some important steps that may cause unpredictable behavior later on. Remember that themes load at a later time than plugins, therefore, we want to include the code in a plugin and *not* a theme.
Remember that at this time, the WordPress Loop has not been created yet. Therefore, we will not have access to any loop objects or functions.
Lines 6,7 – Load background plugin for all administration pages. This will give us access to plugin objects and menus while in admin mode.
Lines 9 to 14 – Check for the address of pages where we want to add our plugin, and include the Shiba Backgrounds main file for those pages.
In this way, my Shiba Backgrounds plugin gets loaded on this page, but not in most other pages of my blog.
Finally, it should be noted that the code above simply loads a plugin for a given set of pages. The plugin is left unactivated in the normal case, and therefore, its activation function (if one exists) never gets run.
As a result, it may be necessary to activate, then deactive the plugin when we first install it. Some plugins may perform important database operations in the activation phase, that then gets wiped on deactivation; in which case we will need to do additional tweaking to make sure that the activation operations stay in-place.
cla says
Perfect! But at first I was misled, I thought the path / wordpress-theme / was an your alias of / theme .. and nothing happened.. boing! boing! 🙂
Aeros says
Hi, I have tried your way. My problem is i’m using boilerplate to most of my plugins. When i tried to include the plugin php file, it loads the plugin UI but without carrying the enqueued styles and scripts. Do you have any idea why? Thanks
Tom says
How would you change this code to make it so that the plugin is NOT loaded on the listed pages?
-Tom
Rafael says
Hello, I am trying to do this, but I received the following error
Fatal error: Call to undefined method My_Plugin::check_uri()
ShibaShake says
I did not include the check_uri function before because people have their own way of doing this. However, for completeness, I have just included a simple example of this function in the code above. You can replace the function code depending on your setup and your needs.
Rafael says
Perfect, thanks for the reply . In my case I use CDN , is there any way to point to the CDN directory? Some scripts carry with the urls of the CDN .