Skip to content

Instantly share code, notes, and snippets.

@carlodaniele
Last active October 13, 2024 06:19
Show Gist options
  • Save carlodaniele/5b8343dd17a64c69d04459bffad2312c to your computer and use it in GitHub Desktop.
Save carlodaniele/5b8343dd17a64c69d04459bffad2312c to your computer and use it in GitHub Desktop.
A Must-use plugin to filter active plugins in on a per-page basis.
<?php
/**
* @package active-plugins
* @version 1.0
*
* Plugin Name: Active Plugins
* Plugin URI: http://wordpress.org/extend/plugins/#
* Description: This is a development plugin
* Author: Carlo Daniele
* Version: 1.0
* Author URI: https://carlodaniele.it/
*/
// shortcode to list active plugins
add_shortcode( 'activeplugins', function(){
$active_plugins = get_option( 'active_plugins' );
$plugins = "";
if( count( $active_plugins ) > 0 ){
$plugins = "<ul>";
foreach ( $active_plugins as $plugin ) {
$plugins .= "<li>" . $plugin . "</li>";
}
$plugins .= "</ul>";
}
return $plugins;
});
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$is_admin = strpos( $request_uri, '/wp-admin/' );
if( false === $is_admin ){
// filter active plugins
add_filter( 'option_active_plugins', function( $plugins ){
global $request_uri;
$is_contact_page = strpos( $request_uri, '/contact/' );
// change elements according to your needs
$myplugins = array(
"contact-form-7/wp-contact-form-7.php",
"code-snippets/code-snippets.php",
"query-monitor/query-monitor.php",
"autoptimize/autoptimize.php"
);
if( false === $is_contact_page ){
$plugins = array_diff( $plugins, $myplugins );
}
return $plugins;
} );
}
@ezosher
Copy link

ezosher commented Jun 7, 2019

Thanks for this snippet it is working, but I need some help please.
I couldn't change the fact that it disables the plugins on all pages && not like in the conditional statement listed in this code

@malayalidotca
Copy link

Hi @carlodaniele,

I am developing a site where I am using the SEOPress SEO plugin and WPFORO forum plugin. These two don't play well together because WPFORO also has built in forum functionalities and I need to disable SEOPress on my forum pages that starts with https://domain.com/forums*

Now coming to the point, I have no idea how to integrate your code and I was wondering if you are available for PAID integration of this script.

For context:

This is the only exclusion I want - No SEOPress plugin on pages starting with https://domain.com/forums/ and https://domain.com/forums/*

I am using WP-Rocket plugin but the forum pages are excluded

I would appreciate a reply.

Jimmy

@guidomgs
Copy link

Mate you are awesome!

@gghidoni
Copy link

gghidoni commented May 1, 2020

Salve, in $myplugins = array si specificano i plugin da disabilitare in quella determinata pagina o i plugin consentiti? Per specificare l'uri della Home Page basta inserire '/' come indirizzo? Grazie

@tbrock47
Copy link

tbrock47 commented Aug 18, 2020

As it is today, I am finding the code snippet will indeed deactivate the chosen plugins but will never reactivate them. Accessing the correctly associated URL does not turn them back on. You have to go into the admin panel and reactivate the plug-in after removing this code.

WordPress 5.4
PHP 7.4

@bobbingwide
Copy link

As it is today, I am finding the code snippet will indeed deactivate the chosen plugins but will never reactivate them. Accessing the correctly associated URL does not turn them back on. You have to go into the admin panel and reactivate the plug-in after removing this code.

I experienced a similar problem when I was developing a very similar plugin. The NextGEN Gallery plugin was updating the active_plugins option at shutdown. My workaround was to run this at shutdown before NextGEN ran its logic.

do_action( 'activate_plugin', __FILE__);

@RayHollister
Copy link

Has anyone been able to get this to work with in_category() or has_term()? I am trying to make a MU plugin that disables plugins one posts and pages in certain categories.

@mcknightd
Copy link

mcknightd commented May 6, 2022

Has anyone been able to get this to work with in_category() or has_term()? I am trying to make a MU plugin that disables plugins one posts and pages in certain categories.

@ryanhollister I believe the plugins will already be loaded before those things are determined. Parsing the page URL might be a workaround for categories if your permalink settings include the category name.

@kevin25
Copy link

kevin25 commented Oct 8, 2022

how about to unload plugins for product page with WooCommerce?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment