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?
Filter
option_active_plugins
. You can change the result ofget_option()
here without actually changing the database.Background
wp_get_active_and_valid_plugins()
callsget_option( 'active_plugins', array() )
to get the active plugins. Inget_option( $option, $default = false )
we find this filter:So the resulting name for our filter is
option_active_plugins
.I’m not sure if this will help you with your ajax call, but Plugin Organizer is a well-supported plugin that enables the selective loading of other plugins based on URL. It works great on my site.