Forked from damiencarbery/disable-plugins-by-url.php
Created
December 29, 2020 05:14
-
-
Save gianghl1983/353d1cb2cffe6ad96c72d727e9835d6f to your computer and use it in GitHub Desktop.
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 *only* 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 to disable on. | |
'plugindir/pluginfile.php' => array( '/dashboard/' ), | |
); | |
$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 ) { | |
//echo( '<p>Permitted list set so return it.</p>' ); | |
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." ); | |
$disable_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'] ); | |
//echo( "<p>Check if $whitelist_url starts with ".$_SERVER['REQUEST_URI'] . '</p>' ); | |
//echo( "<p>Check if ".$_SERVER['REQUEST_URI']." starts with $whitelist_url</p>" ); | |
//if ( 0 !== strpos( $_SERVER['REQUEST_URI'], $whitelist_url ) ) { | |
//if ( 0 === strpos( $whitelist_url, $_SERVER['REQUEST_URI'] ) ) { | |
if ( 0 === strpos( $_SERVER['REQUEST_URI'], $whitelist_url ) ) { | |
//error_log( "It does so enable the plugin for this url." ); | |
//echo( "<p>It does so disable the plugin for this url.</p>" ); | |
$disable_plugin_for_this_url = true; | |
} | |
} | |
if ( $disable_plugin_for_this_url ) { | |
//error_log( "Disable $plugin on ".$_SERVER['REQUEST_URI']."." ); | |
//echo( "<p>Disable $plugin on ".$_SERVER['REQUEST_URI'].".</p>" ); | |
$plugins_to_disable[] = $plugin; | |
} | |
else { | |
//error_log( "Enable $plugin for " . $_SERVER['REQUEST_URI'] ); | |
//echo( "<p>Enable $plugin for " . $_SERVER['REQUEST_URI'] . '</p>' ); | |
} | |
} | |
} | |
// 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