Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
} );
}
@aelsharawi

This comment has been minimized.

Copy link

aelsharawi commented May 7, 2017

it did not work with me (looks like another caching plugin is interfering, I have w3t, varnish)
also, it removes the word "contact" if it is in the main menu and when typing the url contact it redirects to main page

@aelsharawi

This comment has been minimized.

Copy link

aelsharawi commented May 7, 2017

@carlodaniele

This comment has been minimized.

Copy link
Owner Author

carlodaniele commented Jun 2, 2017

Consider that this plugin is just an example supporting a tutorial. It's not meant to be used in production. If needed, the developer should carefully test the plugin on his own installation before putting it at work in production. The plugin has been successfully tested on several installations, but it could not be tested under every conditions.

@luiginica

This comment has been minimized.

Copy link

luiginica commented Jan 6, 2018

This one is for plugins. https://wordpress.org/support/plugin/wp-asset-clean-up/ is for css/js. If you for need css/js you can also use gonzales.
@carlodaniele do you know an alternative plugin like yours? Thank you!

@shirokoweb

This comment has been minimized.

Copy link

shirokoweb commented May 22, 2018

This won't work, you can't send emails (CF7) breaks the plugin

@carlodaniele

This comment has been minimized.

Copy link
Owner Author

carlodaniele commented Mar 9, 2019

This one is for plugins. https://wordpress.org/support/plugin/wp-asset-clean-up/ is for css/js. If you for need css/js you can also use gonzales.
@carlodaniele do you know an alternative plugin like yours? Thank you!

See the following:
https://wordpress.org/plugins/plugin-load-filter/
https://wordpress.org/plugins/plugin-organizer/

@carlodaniele

This comment has been minimized.

Copy link
Owner Author

carlodaniele commented Mar 9, 2019

This won't work, you can't send emails (CF7) breaks the plugin

The code has been reviewed and updated. Check it again

@hellkaim

This comment has been minimized.

Copy link

hellkaim commented Mar 26, 2019

Hi,
Is there a way to manage pages not by URL but by ID (using $page_id = get_queried_object_id(); or similar)? The issue is that on a front page you do not have any URL and in my case it's much easier to manage them by ID's rather then by URL.

Thanks in advance.

@ezosher

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.