wp-load Without Loading the plugins
-
03-03-2021 - |
Question
I am building a 'control panel', for my server and would like to use my Wordpress userbase to control the login functionality, as well as be able to show some posts.
I have set some defines and included wp-load:
define( 'COOKIE_DOMAIN', false );
define( 'DISABLE_WP_CRON', true );
define( 'WP_USE_THEMES', false );
define( 'WP_CACHE', false );
// define( 'SHORTINIT', true );
include( '/home/getyou/www/wp-load.php' );
With shortinit
defined, I am able to utilize some of the functionality, however the user
functionality fails, without it, I notice my w3 Total Cache is caching everything.
Is there a way I can continue to use all of the wordpress functionality, but possibly unload or disable w3 total cache for this external site?
EDIT
Added a mu-plugin
with the following code to disable all plugins based on defining MEDINIT
if ( defined( 'MEDINIT ') && MEDINIT ) {
add_filter( 'option_active_plugins', function ( $plugins ) {
$plug_arr = get_plugins();
foreach( $plug_arr as $k => $v){
unset( $plugins[$k] );
}
return $plugins;
} );
}
However, when I call var_dump( get_option('active_plugins') );
it still loads all plugins.
Solution
You would need to put something like this in a .php file of any name in your must-use plugins folder (/wp-content/mu-plugins/
) so that it loads before all plugins so you can use it to filter the plugin loading...
<?php
if (defined('SHORTINIT') && SHORTINIT) {
add_filter('option_active_plugins', 'shortinit_plugins_filter');
function shortinit_plugins_filter($plugins) {
$noloadplugins = array('w3-total-cache');
foreach ($plugins as $i => $plugin) {
if (in_array($plugin, $noloadplugins)) {unset($plugins[$i]);}
}
return $plugins;
}
}
Alternatively manually include whatever function files you need to make things work as they should.
UPDATE for Multisite
You would need to use a different filter on multisite, something like this should work, eg. for blog ID 2...
<?php
if (defined('SHORTINIT') && SHORTINIT) {
add_filter('site_option_active_plugins', 'shortinit_site_plugins_filter', 10, 3);
function shortinit_site_plugins_filter($plugins, $option, $network_id) {
if ($network_id == 2) {
$noloadplugins = array('w3-total-cache');
foreach ($plugins as $i => $plugin) {
if (in_array($plugin, $noloadplugins)) {unset($plugins[$i]);}
}
}
return $plugins;
}
}
Note if wanted to change sitewide activated plugins you would need to do further filtering of the network wide active plugins, something like:
<?php
if (defined('SHORTINIT') && SHORTINIT) {
add_filter('option_active_sitewide_plugins', 'shortinit_network_plugins_filter');
function shortinit_network_plugins_filter($plugins) {
if (get_current_blog() == 2) {
$noloadplugins = array('w3-total-cache');
foreach ($plugins as $i => $plugin) {
if (in_array($plugin, $noloadplugins)) {unset($plugins[$i]);}
}
}
return $plugins;
}
}