public

Disables specified WordPress plugins

  • Download Gist
disable-plugins.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php
/*
Plugin Name: Disable Plugins
Description: Disables plugins that you specify depending on the value of the WP_LOCAL_DEV constant
Version: 0.2
License: GPL version 2 or any later version
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
 
class CWS_Disable_Plugins {
static $instance;
private $disabled = array();
 
/**
* Sets up the options filter, and optionally handles an array of plugins to disable
* @param array $disables Optional array of plugin filenames to disable
*/
public function __construct( Array $disables = NULL) {
// Handle what was passed in
if ( is_array( $disables ) ) {
foreach ( $disables as $disable )
$this->disable( $disable );
}
 
// Add the filter
add_filter( 'option_active_plugins', array( $this, 'do_disabling' ) );
 
// Allow other plugins to access this instance
self::$instance = $this;
}
 
/**
* Adds a filename to the list of plugins to disable
*/
public function disable( $file ) {
$this->disabled[] = $file;
}
 
/**
* Hooks in to the option_active_plugins filter and does the disabling
* @param array $plugins WP-provided list of plugin filenames
* @return array The filtered array of plugin filenames
*/
public function do_disabling( $plugins ) {
if ( count( $this->disabled ) ) {
foreach ( (array) $this->disabled as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key )
unset( $plugins[$key] );
}
}
return $plugins;
}
}
 
/* Begin customization */
 
if ( defined( 'WP_LOCAL_DEV' ) ) {
if ( WP_LOCAL_DEV ) {
new CWS_Disable_Plugins( array( 'vaultpress.php' ) );
/*
For programmatic disabling, you can initialize the object (e.g. as $_localdev) then do:
$_localdev->disable( 'vaultpress.php' );
*/
} else {
new CWS_Disable_Plugins( array( 'debug-bar/debug-bar.php' ) );
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.