Im trying to disable a plugin on a WordPress frontpage, to make it a bit lighter. I only want it disabled when the front page is displayed.
I got this working code where I put in wp-content/mu-plugins/
It disables one plugin on all pages except a specified /subpage/
This is great, but it’s not exactly what I am after.
How can I change this code to disable a plugin only when viewing the front page ?
Here is the code :
add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/subpage/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
$key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );
if ( false !== $key ) {
unset( $plugins[$key] );
}
}
return $plugins;
}
I also found another code which works for other pages….
But its the same story , I cannot figure out how to make it work for the frontpage, only works on subpages**
$listener_term = '/subpage/';
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';
// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
}
function api_request_disable_plugin( $plugins ) {
$plugins_not_needed = array(
'backupwordpress/backupwordpress.php',
'wordfence/wordfence.php',
'contact-form-7-to-database-extension/contact-form-7-db.php',
'contact-form-7/wp-contact-form-7.php',
'wp-piwik/wp-piwik.php',
'simple-responsive-slider/simple-responsive-slider.php',
'google-sitemap-plugin/google-sitemap-plugin.php',
'category-page-icons/menu-compouser.php',
'easy-fancybox/easy-fancybox.php',
'business-owner-switch/business-owner-switch.php',
'wordpress-seo/wp-seo.php'
);
foreach ( $plugins_not_needed as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
return $plugins;
}
Any ideas?
I guess I should mention that:
- The first example will disable the plugin on all pages except for /subpage/
- The second code will disable all the listed plugins on /subpage/
Neither of them work for frontpage/homepage, no matter what I try.
3
Answers
I didn’t realise you ONLY wanted it to happen on the Front Page – the code snippet suggests you want it on some other pages too.
I was previously suggesting you could use is_home or is_front_page, but at the stage we are doing this, they have not been loaded yet – so an alternative here is to load in the REQUEST_URI and see if it is blank. If it is blank, we will assume we are on the homepage – to cater for installations that may not be at the root, we compare the URL to the site’s relative homepage location – for added safety, we encapsulate it within htmlspecialcharacters (probably not necessary).
Perhaps try something like this (look, it may not be the best way to do it, but it certainly works for me):
If you wanted to do this for multiple plugins, you could change the code to the following:
you could write an mu-plugin, if you don’t have it, create a folder called “mu-plugins” inside the “wp-content” folder, then create a PHP file where you can write and try this code:
Just replace “plugin_folder1/plugin1.php” … with the plugins you want to deactivate
Another option is to install the free plugin Freesoul Deactivate Plugins and you will be able to easily deactivate the plugins you want for each page, post, custom post, and archive.
Here is an abstraction I wrote that allows you to specify a route and pass it a list of plugins. Print out the plugins to a log or the screen to get their names then just add them to the array and they will get filtered.
Another approach to consider would be replacing targets with a whitelist, so you have to define less things per route (if you have a lot of plugins going).
This could be expanded to use an array of needles (routes) that have an array of plugins to remove for even more control.
Something like: