Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Created January 18, 2022 20:06
Show Gist options
  • Save joshuafredrickson/949cc0eb19d8ca2d5d7c23d8d9134ff3 to your computer and use it in GitHub Desktop.
Save joshuafredrickson/949cc0eb19d8ca2d5d7c23d8d9134ff3 to your computer and use it in GitHub Desktop.
Disable WordPress plugins in certain environments
<?php
/**
* Plugin Name: Disable Plugins
* Plugin URI: https://gist.github.com/joshuafredrickson/949cc0eb19d8ca2d5d7c23d8d9134ff3
* Version: 1.0.0
* Description: Disable certain plugins in certain environments
* Author: joshuafredrickson
* Author URI: https://github.com/joshuafredrickson
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
add_action('admin_init', function () {
// Disable plugins only in Development or Staging
disable_plugins([
'autoptimize/autoptimize.php',
'sucuri-scanner/sucuri.php',
], ['development', 'staging']);
});
function disable_plugins(array $plugins = [], array $environments = [])
{
if (defined('WP_ENV') && in_array(WP_ENV, $environments)) {
deactivate_plugins($plugins);
// Notify / override actions on plugin screen to let admin know why it's disabled
add_filter('plugin_action_links', function ($action_links, $plugin_file) use ($plugins, $environments) {
if (in_array($plugin_file, $plugins)) {
$envs = implode(', ', $environments);
$action_links = [
'disabled' => "<small style='color: #333;'><b style='color: #b32d2e;'>Disabled in {$envs}</b>. Config in `mu-plugins/disable-plugins.php`</small>"
];
}
return $action_links;
}, 10, 5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment