Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disables specified WordPress plugins when doing local development
<?php
/*
Plugin Name: Disable plugins when doing local dev
Description: If the WP_LOCAL_DEV constant is true, disables plugins that you specify
Version: 0.1
License: GPL version 2 or any later version
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
class CWS_Disable_Plugins_When_Local_Dev {
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' ) && WP_LOCAL_DEV ) {
new CWS_Disable_Plugins_When_Local_Dev( array( 'vaultpress.php' ) );
/*
For programmatic disabling, you can initialize the object (e.g. as $_localdev) then do:
$_localdev->disable( 'vaultpress.php' );
*/
}
@Rarst

This comment has been minimized.

Show comment
Hide comment
@Rarst

Rarst Dec 28, 2012

Doesn't catch network-activated plugins, since they are stored and loaded separately. See my fork https://gist.github.com/4402927 for attempt at fix.

Rarst commented Dec 28, 2012

Doesn't catch network-activated plugins, since they are stored and loaded separately. See my fork https://gist.github.com/4402927 for attempt at fix.

@tamarazuk

This comment has been minimized.

Show comment
Hide comment
@tamarazuk

tamarazuk Sep 7, 2013

Thank you for this! It sure has been helpful.

I have a couple of questions that arose when I noticed that I have to reset my InfiniteWP API key whenever I push a local database to production. I am using WP Migrate DB Pro to move the database back and forth. There are cases when you have to push changes made to the database locally to production, but this also means that your deactivated plugins go with it. This also includes my backup plugin, which I have to remember to reactivate each time.

Is there any way around this? I was thinking of adjusting the code to activate an array of plugins if WP_LOCAL_DEV is false (thus we are in production) but then I would have to remove the site from InfiniteWP and re-add it whenever I do a push which could get cumbersome.

Is there a reason the core deactivate_plugins( $plugins, $silent = false, $network_wide = null ) function is not used?

tamarazuk commented Sep 7, 2013

Thank you for this! It sure has been helpful.

I have a couple of questions that arose when I noticed that I have to reset my InfiniteWP API key whenever I push a local database to production. I am using WP Migrate DB Pro to move the database back and forth. There are cases when you have to push changes made to the database locally to production, but this also means that your deactivated plugins go with it. This also includes my backup plugin, which I have to remember to reactivate each time.

Is there any way around this? I was thinking of adjusting the code to activate an array of plugins if WP_LOCAL_DEV is false (thus we are in production) but then I would have to remove the site from InfiniteWP and re-add it whenever I do a push which could get cumbersome.

Is there a reason the core deactivate_plugins( $plugins, $silent = false, $network_wide = null ) function is not used?

@AlchemyUnited

This comment has been minimized.

Show comment
Hide comment
@AlchemyUnited

AlchemyUnited Oct 8, 2013

Call me crazy but I'm thinking I'd prefer to maintain an active list, instead of a disable list. That is, presume everything should be disabled and then only activate those in the list. This way anything new starts as off, instead of on. Which to me, while conservative, makes the most sense.

AlchemyUnited commented Oct 8, 2013

Call me crazy but I'm thinking I'd prefer to maintain an active list, instead of a disable list. That is, presume everything should be disabled and then only activate those in the list. This way anything new starts as off, instead of on. Which to me, while conservative, makes the most sense.

@strider72

This comment has been minimized.

Show comment
Hide comment
@strider72

strider72 Feb 27, 2015

Reminds me of something I did, where you set an array of plugins that cannot be deactivated in Admin. Basically turns regular plugins into MU via a similar array manipulation to what you did.

strider72 commented Feb 27, 2015

Reminds me of something I did, where you set an array of plugins that cannot be deactivated in Admin. Basically turns regular plugins into MU via a similar array manipulation to what you did.

@pbiron

This comment has been minimized.

Show comment
Hide comment
@pbiron

pbiron Jun 1, 2018

Thanx for this!

The "Get it here" link in your blog post mentioning this plugin has an incorrect URL to this gist...probably because github changed the structure of gist URLs since you wrote that post. You should update that link.

pbiron commented Jun 1, 2018

Thanx for this!

The "Get it here" link in your blog post mentioning this plugin has an incorrect URL to this gist...probably because github changed the structure of gist URLs since you wrote that post. You should update that link.

@pbiron

This comment has been minimized.

Show comment
Hide comment
@pbiron

pbiron Jun 24, 2018

See my fork of Rarst's fork of this, enable-disable-plugins-when-doing-local-dev.php that adds the capability to enable as well as disable plugins locally.

pbiron commented Jun 24, 2018

See my fork of Rarst's fork of this, enable-disable-plugins-when-doing-local-dev.php that adds the capability to enable as well as disable plugins locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment