Last active
December 9, 2022 14:28
-
-
Save damiencarbery/dbed391405a2f5e4cba378d4a8fe8ee2 to your computer and use it in GitHub Desktop.
Description: Disable selected plugins for specific IP addresses - very useful when debugging plugin clashes: k: https://www.damiencarbery.com/2018/03/disable-selected-plugins-for-specified-ips/
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
[10-Mar-2018 16:08:01 UTC] | |
array ( | |
//query-monitor/query-monitor.php, | |
//hello.php, | |
); |
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: Disable Selected Plugins | |
Plugin URI: https://www.damiencarbery.com | |
Description: Disable selected plugins for specific IP addresses - very useful when debugging plugin clashes. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
class DisableSelectedPlugins { | |
private $active_ip; // Limit plugin to use by these IP addresses. | |
private $create_debug_list; // Whether to create a debug list. | |
private $call_count; // Count of times filter function called so that debug messages not generated too often. | |
public function __construct() { | |
$this->active_ip = array( '12.34.56.78', '34.56.78.90' ); | |
$this->create_debug_list = true; | |
$this->call_count = 0; | |
} | |
public function init() { | |
if ( in_array( $_SERVER['REMOTE_ADDR'], $this->active_ip ) ) { | |
add_filter( 'option_active_plugins', array( $this, 'modify_active_plugins' ) ); | |
} | |
} | |
public function modify_active_plugins( $plugin_list ) { | |
$this->call_count++; // In some cases this function was called multiple times but I cannot reproduce this. | |
// This variable prevents writing plugin list to error_log multiple times. | |
// Create a commented list of active plugins. | |
if ( 1 == $this->call_count && $this->create_debug_list ) { | |
$plugin_var = "\narray (\n"; | |
foreach ( $plugin_list as $plugin ) { | |
$plugin_var .= ' //\'' . $plugin . "\',\n"; | |
} | |
$plugin_var .= ');'; | |
error_log( $plugin_var ); | |
$this->create_debug_list = false; // Don't create again | |
} | |
// Change the returned list for when in the dashboard or frontend. | |
if ( is_admin() ) { | |
return $plugin_list; | |
} | |
else { | |
return $plugin_list; | |
} | |
} | |
} | |
$DisableSelectedPlugins = new DisableSelectedPlugins(); | |
$DisableSelectedPlugins->init(); |
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 | |
// Change 'return $plugin_list' to the array written to error_log (or debug.log) | |
// Example: | |
return $plugin_list; | |
// changed to: | |
return array ( | |
//query-monitor/query-monitor.php, | |
//hello.php, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment