Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class To Disable WordPress Plugins By URL - Disable specified plugins except for certain pages - enhanced with caching. http://www.damiencarbery.com/2019/03/class-to-disable-wordpress-plugins-by-url/
<?php
/*
Plugin Name: Class To Disable WordPress Plugins By URL
Plugin URI: http://www.damiencarbery.com/2019/03/class-to-disable-wordpress-plugins-by-url/
Description: Disable specified plugins except for certain pages - enhanced with caching.
Version: 0.1
Author: Damien Carbery
Author URI: http://www.damiencarbery.com/
*/
class DisablePluginsByUrl {
// A reference to an instance of this class.
private static $instance;
// List of plugins to disable and the excluded urls.
private $disable_plugins;
// List of permitted plugins.
private $permitted_plugins;
// Whether list of permitted plugins has been populated.
private $permitted_list_set;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new DisablePluginsByUrl();
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->permitted_plugins = null;
$this->permitted_list_set = false;
$this->disable_plugins = array (
// Plugin Name => Urls *not* to disable on.
'contact-form-7/wp-contact-form-7.php' => array( '/wp-json/contact-form-7/', '/contact/' ), // /wp-json url needed to send messages
//'advanced-custom-fields-pro/acf.php' => array( '/page-one/', '/page-two/', '/page-three/' ),
'query-monitor/query-monitor.php' => array( '/standalone/2018/' ),
);
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
if ( ! is_admin() ) {
add_filter( 'option_active_plugins', array( $this, 'disable_plugins_per_page' ) );
}
}
// Determine whether to permit or prohibit the plugins for this url.
public function disable_plugins_per_page( $plugin_list ) {
// Short circuit this function if the list of permitted
// plugins has already been determined.
if ( $this->permitted_list_set ) {
return $this->permitted_plugins;
}
$plugins_to_disable = array();
foreach ( $plugin_list as $plugin ) {
// If the active plugin is in the list that we are
// interested in then check its list of excluded urls.
if (true == array_key_exists( $plugin, $this->disable_plugins ) ) {
//error_log( "Found $plugin in list of active plugins." );
$enable_plugin_for_this_url = false;
//error_log( "Check $plugin urls: " . var_export( $this->disable_plugins[ $plugin ], true ) );
foreach ( $this->disable_plugins[ $plugin ] as $whitelist_url ) {
//error_log( "Check if $whitelist_url starts with ".$_SERVER['REQUEST_URI'] );
if ( 0 === strpos( $_SERVER['REQUEST_URI'], $whitelist_url ) ) {
//error_log( "It does so enable the plugin for this url." );
$enable_plugin_for_this_url = true;
}
}
if ( ! $enable_plugin_for_this_url ) {
//error_log( "Disable $plugin on ".$_SERVER['REQUEST_URI']."." );
$plugins_to_disable[] = $plugin;
}
else {
//error_log( "Enable $plugin for " . $_SERVER['REQUEST_URI'] );
}
}
}
// If there are plugins to disable then remove them from the list,
// otherwise return the original list.
if ( count( $plugins_to_disable ) ) {
$this->permitted_plugins = array_diff( $plugin_list, $plugins_to_disable );
$this->permitted_list_set = true;
return $this->permitted_plugins;
}
else {
$this->permitted_plugins = $plugin_list;
$this->permitted_list_set = true;
return $this->permitted_plugins;
}
}
}
$DisablePluginsByUrl = new DisablePluginsByUrl;
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.