WordPress.org

Ready to get started?Download WordPress

Ideas

Selective Plugin loading

  1. Justin Tadlock
    Member

    12345

    I believe there's a lot of misunderstanding happening in this thread, which is why we've all been arguing back and forth. I know I've been arguing against the proposed idea, but I'm a fan of contextual/dynamic loading idea in general. My arguments have not been rooted in "ideology" though. I'm talking about actual, real-world use here and problems presented with the idea.

    The original idea in this thread is about the loading of an entire plugin's PHP files. Styles and scripts are a different thing, but I'll get to those later.

    The idea is to selectively load PHP files on specific pages. I know that many people don't understand the order hooks are fired in, so I'll try to keep this simple. WordPress fires "hooks" at certain points in the page load. The required hook here for conditional loading is template_redirect. This is the point WordPress knows what page you're on. For example, you can start using conditional tags like is_page(), is_category(), and so on here. This hook is fired later than many other hooks in WordPress. If a plugin needs one of those earlier hooks (many do), it'd be too late in the process to load the plugin. Thus, breaking the functionality of the plugin.

    And, we're just talking about the front end of the site here. I didn't even get into the admin area stuff.

    I don't know how to explain it any simpler than that. It's not that I don't feel all of your pain (I use some crappy plugins too), but I'm just looking at the fact that this type of loading is impossible for all plugins.

    Style and scripts

    Since this was brought up, I thought I'd address it. Styles and scripts are a different beast than PHP files, albeit a much smaller beast in terms of the idea of this topic.

    Assuming all plugins correctly use wp_enqueue_style() and wp_enqueue_script() to load their styles and scripts, I don't see any reason it wouldn't be possible to code an interface for controlling when these files are loaded. Not all plugins use this system correctly, so there's nothing you can do about those.

    The only problem with this is that I don't see it as core material. It goes against the core philosophy of make decisions, not options. What I'd really like to see is a plugin that provided an easy-to-use interface for controlling the loading of these files. If no one wants to tackle this, I'll be more than happy to look into the feasibility of doing this in a month or two after knocking out some other projects.

    Posted: 3 years ago #
  2. gazouteast
    Member

    12345

    Justin

    As a starting point, even some form of very early hook that allowed the decision of
    is_admin_only plugin
    and
    is_public_only_plugin
    would be a great start to cutting down on universal loading.

    Just as an example (or two) take the "Hide Dashboard Options" plugin. That has absolutely no need to load on the public side of the blog, because it only affects admin, and only when the user role is subscriber.

    Another is Akismet - it has zero need to load in admin unless the akismet stats page or the dashboard widget to show posts, comments, and spam is loaded. It should not be loading on every admin page.

    I understand that "as the core stands now" it's impossible to provide early enough receptors to filter plugin loading, but it is maybe a project for building out with a view to WP 4.x or 5.x - it's going to be needed even more by then. The history of software development shows that the more aged an application or script, the bigger it gets, and the more resources it consumes, while the history of hosting shows that resources rarely get increased in the packages offered, whilst simultaneously, the underlying OS and hosting control panels consume more and more of the initially advertised resources.

    There will come a point when WP and plugins/themes will simply consume too many resources for it to run on anything but a dedicated server, and at that point it will become too expensive to host for anything except full blown corporate multinationals.

    I'm seeing this already - in three years, my wordpress hosting has gone from a small and cheap hosting account to a pseudo-unlimited account, to several "reseller" accounts to now having to engage VPS's just to be able to run my sites as WP and the plugins have grown hungrier for resources.

    Where once I paid a total of GBP 25.00 a year to host 5 heavy sites, I now have to pay double that a month for 7 heavy sites and a bunch of in-development sites. I can no longer offer client's first year hosting free on the hostings I already have - they have to get their own hosting accounts from the get go - this is all resources related.

    WP needs to start taking control of steering the options for plugins to load according to which half of the site is loading (especially with MS installs) and ideally at a page type level too. I know it's not an immediate option currently, but it needs done for the future.

    Posted: 3 years ago #
  3. zafrir_ron
    Member

    12345

    Dear Friends

    Thank you for all your feedback on this.
    after reading all the comments, I can say that the current status of all the plugins I use and know (including the most popular as mentioned here) are taking a great load on url calls not relevant, loading js and css files on none relevant pages.
    I use 58 plugins on single site (number of plugins not relevant since they can be small or big and some used just to save development time from my own private extension of 2500 lines of code).

    So regardless of the very good point raised by some of the commentators here, I'm using this for over 6 month now, profiling measurements of cpu and memory use convinced me to continue to use it, since its located in single point in the code its very easy to measure performance enabling/disabling the selective loading (remember to check on relevant pages (urls).

    some asked to share, so here it is (bare in mind that I did it internally and not for generic use as is. you have to understand what you are doing here...as I mentioned before it should be developed as a plugin "control manager" to enable us to take control regardless of plugin writers assumptions of their plugin use.

    the basic data structure is array of plugin names, each element include single array element 'include' or 'exclude' array containing uri regular expressions (in the code sample I included an array sample).

    in my-hacks.php

    $uri = $_SERVER['REQUEST_URI']; //temporary holding requested uri
    $ plugins_load = apc_fetch('plugins_load'); //I'm using apc opcode cache
    if (!$plugins_load ) {
    $plugins_load = array(
    'wp-e-commerce/wp-shopping-cart.php' => array(
    'include' => array('/index.php/','/other-usrl-reg-exp/','/other-url-reg-exp/','/wp-admin/')),
    'add-from-server/add-from-server.php' => array(
    'include' => array('/wp-admin/')),
    'exploit-scanner/exploit-scanner.php' => array(
    'include' => array('/wp-admin/')),
    'ozh-admin-drop-down-menu/wp_ozh_adminmenu.php' => array(
    'include' => array('/.*wp-admin.*/')),
    'support-tickets/support-tickets.php' => array(
    'include' => array('/contact-us/','/wp-admin/')),
    'tinymce-advanced/tinymce-advanced.php' => array(
    'include' => array('/wp-admin/')) ,
    'wp-dbmanager/wp-dbmanager.php' => array(
    'include' => array('/wp-admin/')) ,
    'sitemap-generator/sitemap-generator.php' => array(
    'include' => array('/wp-admin/','/site-map/')),
    'font-resizer/font-resizer.php' => array(
    'exclude' => array('/^\/$/','/exclude-url/','/other-excluded-url/')),
    'sociable/sociable.php' => array(
    'exclude' => array('/^\/$/','/url1/','/url2/')),
    'wp-email/wp-email.php' => array(
    'exclude' => array('/^\/$/','/url1/','/url2/')),
    .
    .
    .
    add elemnts for every plugin you need to control
    .
    .
    .
    ) ;
    apc_store('plugins_load', $plugins_load); //I'm using APC
    }
    //this is the filter function
    function mkp_load_plugin($current_plugin) {
    global $plugins_load; //temporary global var holding our options
    global $uri; //temporary var holding the current uri
    $include = true; //default to include plugin
    if (!isset($plugins_load[$current_plugin])) return $include; //plugin not in the options list => plugin included
    $incexc = array_keys($plugins_load[$current_plugin]); //extract include/exclude status from array (get the key array)
    $isuri = in_array(1,array_map("mkp_check_match",$plugins_load[$current_plugin][$incexc[0]])); // check if uri matched to any elemnts
    if (($incexc[0] == 'exclude' && $isuri)||($incexc[0] == 'include' && !$isuri )) $include = false; //if uri found in excluded list or not found in iclude list => plugin excluded
    return $include;
    }
    function mkp_check_match($val) { global $uri; return preg_match($val,$uri); }//array_map callback function to preg_match items


    in wp-settings.php add single line in the plugin validation and loading loop

    if ( validate_file($plugin) // $plugin must validate as file
    || '.php' != substr($plugin, -4) // $plugin must end with '.php'
    || !file_exists(WP_PLUGIN_DIR . '/' . $plugin) // $plugin must exist
    || !mkp_load_plugin($plugin) // **** here selective plugin load
    )

    Posted: 3 years ago #
  4. zafrir_ron
    Member

    12345

    Dear Greenshady, thanks for your comments.

    Just forgot to explain....you wrote

    it'd be too late in the process to load the plugin. Thus, breaking the functionality of the plugin.

    Well...this is VERY early in the process, the plugin file is not even accessed or loads!

    The decision here is if to look for the plugin file based on the URI!

    As said its not a plugin coding its a core coding, that's why it should go into WP core.

    I invite anyone to write the simple admin control page:
    since WP core is loaded, its reading the active plugin list by get_option( 'active_plugins' ), write a form page with plugin list and "include/exclude" radio select and single line for comma separated reg-exp of urls, write the result back as option (see data structure).

    I guess when I will find few hours to spare I'll code it and update here.

    Posted: 3 years ago #
  5. Justin Tadlock
    Member

    12345

    @zafrir_ron

    If you have code that you think would work for core, you should be posting it on Trac:
    http://core.trac.wordpress.org

    Add a ticket there explaining what you want to do and attach your code as a file.

    Just be sure to leave a link back here with the ticket that you create on Trac.

    Posted: 3 years ago #
  6. ucfknight10
    Member

    if a plugin is slowing your site down, dont use it. find another one. by doing this you will force the plugin author to update their script to be more efficient and load only when it is needed. as a plugin developer also, i take great care to make my plugins as efficient as possible, and to load only when needed.

    and the clients i develop sites for, if selectively loading plugins per post/page was possible, i know they would crash the site constantly, and come crying to me for help, or blame me for the problem. i think that bugging the plugin author is the best way to affect the changes you are wanting.

    Posted: 3 years ago #
  7. zafrir_ron
    Member

    12345

    Thanks for your comment

    If you check the most popular plugins on the net including the >1000,000 installations you will learn that this is not an issue only of plugin design.
    I'm not in the business of education of plugin authors, so I'm only looking for the best ways to implement what I need.
    Many plugins only written for single page and admin, no use to even read it from the disk.

    If you read my explanation, you will find that its possible to selectively load plugin without crashing the site...I also can inform you that I'm working like this for 6 month with over 60 plugins now...

    The idea behind this is to give site and WP owners the control over selective loading of plugins. If you look for education of plugin developers it should be to think and mark where your plugin should be loaded, instead of all plugins code always loads for every page load.

    Posted: 3 years ago #
  8. enailor
    Member

    12345

    @zafrir_ron
    Thank you for posting your code. I do hope you submit this to the Trac for consideration as @Greenshady suggested.

    This does appear to be rather cumbersome for general use, so I do hope someone can take this and turn it into either a solid plugin or find a way to add this into the core (this would be beyond my capabilities at this point). It would be very helpful.

    @ucfknight10
    I commend your approach to writing effective plugins, but I have to disagree in a couple places with you.

    First off, constantly bugging plugin developers may not be the best option. Most of them offer their plugins for free, so bugging someone over something that you did not pay for and not offering to pay their for their time to fix what you consider to be a problem is a bit much. If the plugin developer wants to continue the plugin development, its fine to request a feature or programming change, but we must always keep in mind that they are doing so in their free time.

    Also, there are a number of plugins that are just the best at what they do, but at the same time can bloat your website with code not needed on each page load. Having a way to curb that would be a great option. And I do believe that there are enough bright minds in this community that can make this work without it breaking all the time.

    Now, I do agree with you on being selective about the plugins you use. I prefer to not use plugins unless I have to. If I can add the same functionality to my theme's function.php file, then I have more control over things. And I don't have to worry about a developer abandoning a plugin down the road. Of course, this means you have to understand the functionality of a plugin to begin with, and some are way to in depth or big to simply add to your function.php file. So there are still times we need to use the plugins.

    @all
    I do hope that this idea gets substantial consideration. I also hope that plugin developers will see this as a request to try and write smart code that only adds to the page load when needed.

    Posted: 3 years ago #
  9. garnser
    Member

    Hi all,

    Even though this thread is a bit outdated an old colleague of mine just released a plugin which enabled on to selectively choose which plugins to load for each page/category etc.

    http://wordpress.org/extend/plugins/selective-loading/

    Posted: 1 year ago #
  10. garnser
    Member

    A colleague of mine and I have developed a plugin (oh the irony) to deal with this: feel free to give it a spin.

    http://wordpress.org/extend/plugins/selective-loading/

    Posted: 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    25 Votes
  • Status

    This is not a core suggestion