As a WordPress developer, debugging live websites can be challenging, especially when you don’t want to deactivate all plugins for all users.
In such cases, you can use a clever code snippet to selectively deactivate certain plugins based on your remote IP address.
This technique allows you to test and troubleshoot your website without affecting the experience of other visitors.
1. Set Up IP-Based Debugging
Begin by using your IP address as a condition to control which parts of your code should execute. In this example, we’re checking if the remote IP address matches specific values ([YOUR_IP_ADDRESS] or '127.0.0.1')
. If the condition is met, the code inside the block will run.
if ( $_SERVER['REMOTE_ADDR'] == [YOUR_IP_ADDRESS] || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
// Your code for deactivating specific plugins goes here.
}
127.0.0.1
represents a local IP address, making it a versatile condition that can be employed for debugging purposes even when working on your local development environment.
NOTE: Ensure to replace [YOUR_IP_ADDRESS]
with your actual IP address.
2. Retrieve a List of Active Plugins
To deactivate specific plugins, you need to obtain a list of all currently active plugins. You can achieve this by using the get_option('active_plugins')
function:
if ( $_SERVER['REMOTE_ADDR'] == [YOUR_IP_ADDRESS] || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
var_dump( get_option('active_plugins') );
}
Now, you have an array containing the file paths of all active plugins on your WordPress website.
array(35) { [3]=> string(61) "back-in-stock-notifier-for-woocommerce/cwginstocknotifier.php" [4]=> string(19) "bigblue/bigblue.php" [6]=> string(39) "crisp-woocommerce/crisp-woocommerce.php" [7]=> string(31) "envato-market/envato-market.php" [8]=> string(53) "facebook-for-woocommerce/facebook-for-woocommerce.php" [9]=> string(45) "flexible-coupons-pro/flexible-coupons-pro.php" [10]=> string(39) "flexible-shipping/flexible-shipping.php" [11]=> string(23) "goya-core/goya-core.php" [12]=> string(27) "js_composer/js_composer.php" [13]=> string(15) "kirki/kirki.php" [14]=> string(19) "klaviyo/klaviyo.php" [15]=> string(37) "mailchimp-for-wp/mailchimp-for-wp.php" [16]=> string(21) "meta-box/meta-box.php" [17]=> string(67) "mollie-payments-for-woocommerce/mollie-payments-for-woocommerce.php" [18]=> string(27) "ninja-forms/ninja-forms.php" [19]=> string(47) "packlink-pro-shipping/packlink-pro-shipping.php" [22]=> string(41) "return-manager-woo/return-manager-woo.php" [23]=> string(23) "revslider/revslider.php" [24]=> string(47) "show-current-template/show-current-template.php" [25]=> string(25) "show-hooks/show-hooks.php" [26]=> string(14) "vimeo/Core.php" [27]=> string(32) "wc-ajax-product-filter/wcapf.php" [28]=> string(30) "woo-advanced-discounts/wad.php" [29]=> string(41) "woo-discount-rules/woo-discount-rules.php" [30]=> string(47) "woo-order-export-lite/woo-order-export-lite.php" [31]=> string(40) "woo-product-feed-pro/woocommerce-sea.php" [32]=> string(67) "woo-sale-discount-scheduler/woocommerce-sale-discount-scheduler.php" [33]=> string(49) "woo-variation-swatches/woo-variation-swatches.php" [34]=> string(49) "woocommerce-coupon-box/woocommerce-coupon-box.php" [35]=> string(91) "woocommerce-gateway-paypal-express-checkout/woocommerce-gateway-paypal-express-checkout.php" [36]=> string(85) "woocommerce-google-analytics-integration/woocommerce-google-analytics-integration.php" [37]=> string(80) "woocommerce-pdf-invoices-packing-slips/woocommerce-pdf-invoices-packingslips.php" [38]=> string(73) "woocommerce-sendinblue-newsletter-subscription/woocommerce-sendinblue.php" [41]=> string(27) "wp-crontrol/wp-crontrol.php" [43]=> string(34) "yith-woocommerce-wishlist/init.php" }
3. Deactivate the Desired Plugins
It’s essential to utilize the option_active_plugins
filter to modify the list of active plugins dynamically. This filter allows you to intercept and manipulate the plugin list before it’s retrieved by WordPress.
if ( $_SERVER['REMOTE_ADDR'] == [YOUR_IP_ADDRESS] || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
add_filter( 'option_active_plugins', function( $active_plugins ){
return $active_plugins;
});
}
Next, create an array named $plugins_to_deactivate
that lists the file paths of the plugins you want to deactivate:
if ( $_SERVER['REMOTE_ADDR'] == [YOUR_IP_ADDRESS] || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
add_filter( 'option_active_plugins', function( $active_plugins ){
$plugins_to_deactivate = array(
"back-in-stock-notifier-for-woocommerce/cwginstocknotifier.php",
"bigblue/bigblue.php",
"crisp-woocommerce/crisp-woocommerce.php",
);
return $active_plugins;
});
}
Finally, use a foreach loop to iterate through the $plugins_to_deactivate
array and remove the corresponding plugins from the $active_plugins
array:
if ( $_SERVER['REMOTE_ADDR'] == [YOUR_IP_ADDRESS] || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
add_filter( 'option_active_plugins', function( $active_plugins ){
$plugins_to_deactivate = array(
"back-in-stock-notifier-for-woocommerce/cwginstocknotifier.php",
"bigblue/bigblue.php",
"crisp-woocommerce/crisp-woocommerce.php",
);
$keys = array_flip($active_plugins);
foreach ( $plugins_to_deactivate as $plugin ) {
if ( isset( $keys[$plugin] ) ) {
unset( $active_plugins[ $keys[$plugin] ] );
}
}
return $active_plugins;
});
}
4. Place the Code Inside the “mu-plugins” Directory
For this debugging technique to work effectively, you’ll need to create a custom PHP file inside the “mu-plugins” (Must-Use Plugins) directory. “mu-plugins” are a special type of plugin that is automatically loaded by WordPress before the regular plugins, ensuring that your debugging logic takes precedence.
Here’s how to do it:
- Inside the “wp-content” directory, locate the “mu-plugins” folder. If it doesn’t exist, you can create it.
- Inside the “mu-plugins” folder, create a new PHP file (e.g.,
debug-plugins.php
). - Paste the entire code snippet into
debug-plugins.php
.
Now, your custom code for deactivating specific plugins based on your IP address will be executed early in the WordPress loading process, allowing you to debug and test your site without affecting the user experience.