Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out some kind of mechanism to load plugins on demand, depending on the page url, to improve performance.

My primary concern is that I have some admin-ajax.php calls that are recurring while the user is active on the page. I tried profiling these calls, and discovered that the majority of plugins loaded for the ajax call were unnecessary.

While I could use is_admin() to identify an ajax call, I wish to leave WordPress and plugin code intact. Plus, is_admin() does not differentiate between an ajax call and admin panel.

I've been looking into the plugin loading procedure. I only saw 'muplugins_loaded', 'plugins_loaded' actions, no filters to tap into the loading process. I checked wp_get_active_and_valid_plugins and it appears the only way to change plugin loading is to update_option('active_plugins') explicitly, which is not a viable option. Other things are hard coded.

(I'd like to mention that this plugin http://wordpress.org/extend/plugins/selective-loading/ use the update_option method, which in my opinion is only suitable in a non-ajax environment, or otherwise the option change wouldn't be atomic.)

I'm wondering if there was still some way to implement selective plugin loading?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Filter option_active_plugins. You can change the result of get_option() here without actually changing the database.

if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    add_filter( 'option_active_plugins', 'disable_plugins_temporary' );

function disable_plugins_temporary( $plugins )
{
    // unset plugins you don't need, then

    return $plugins;
}

Background

wp_get_active_and_valid_plugins() calls get_option( 'active_plugins', array() ) to get the active plugins. In get_option( $option, $default = false ) we find this filter:

return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );

So the resulting name for our filter is option_active_plugins.

share|improve this answer
add comment

Give that the selective-loading plugin bases the loading of the URLs to the site it should be viable to use with AJAX as well, might be wrong though.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.