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?
2 s
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
.