Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Plugin / How to Selectively Load Plugins for Specific Pages

How to Selectively Load Plugins for Specific Pages

by ShibaShake 6 Comments

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. cla says

    March 21, 2018 at 11:05 am

    Perfect! But at first I was misled, I thought the path / wordpress-theme / was an your alias of / theme .. and nothing happened.. boing! boing! 🙂

    Reply
  2. Aeros says

    March 13, 2018 at 4:25 pm

    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

    Reply
  3. Tom says

    November 13, 2015 at 7:45 am

    How would you change this code to make it so that the plugin is NOT loaded on the listed pages?

    -Tom

    Reply
  4. Rafael says

    April 13, 2015 at 7:29 pm

    Hello, I am trying to do this, but I received the following error

    Fatal error: Call to undefined method My_Plugin::check_uri()

    Reply
    • ShibaShake says

      April 13, 2015 at 11:37 pm

      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.

      Reply
      • Rafael says

        April 14, 2015 at 5:43 am

        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 .

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...
  • How to Add Admin Columns in WordPress (7)
    • Andy Globe
      - Hi Friends, I am facing two problem in WordPress admin1. Load custom CSS for wp-admin on user-role base (editor) 2. ...
  • Example blog front-page using excerpts and the Shiba Theme.Optimize Your WordPress Plugins with Tags (5)
    • DF
      - thanks, i went the other way and added a filter if pages I wanted.
  • WordPress Search Widget – How to Style It (57)
    • Nelson
      - Tanks master - Fine
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (59)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...

Copyright © 2022 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·