How to Disable WordPress Plugins From Loading on Specific Pages and Posts
Shows list of active running plugins – to start to break down which we should turn off on what page:
[activeplugins]
With that being done, we can go a step further and add or remove plugins programmatically by taking advantage of the option_active_plugins
filter. This filter belongs to the option_$option_name group of filters, which allow to filter any option after it’s been retrieved from the database. Since all active plugins are stored in wp_options
table where option_value
is active_plugins
, the option_active_plugins
filter provides a way to programmatically activate or deactivate plugins.
So we can activate a plugin programmatically. Say, as an example, you want to activate the ACF plugin. Here is the code:
[code class=”php”]add_filter( ‘option_active_plugins’, function( $plugins ){
$myplugin = "advanced-custom-fields/acf.php";
if( !in_array( $myplugin, $plugins ) ){
$plugins[] = $myplugin;
}
return $plugins;
} );
[/code]The code above simply adds the plugin to the list of active plugins on every page of our website. Not very useful, but you get the point.
Moreover, the plugin should load before any other plugin, otherwise, our code could not work as expected. In order to prioritize our plugin load, we have to add our script in a Must-use plugin.