Revision 331 is a pre-publication revision. (Viewing current revision instead.)

Disable specified WordPress plugins when doing local development

Look up Mark Jaquith for more info.
[php]<?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 to both single site and multisite plugin lists.
		add_filter( 'option_active_plugins', array( $this, 'do_disabling' ) );
		add_filter( 'site_option_active_sitewide_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 and site_option_active_sitewide_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 ) {
				if( current_filter() == 'option_active_plugins' )
					$key = array_search( $plugin, $plugins );
				else
					$key = !empty( $plugins[$plugin] ) ? $plugin : false;
				if( false !== $key )
					unset( $plugins[$key] );
			}
		}
		return $plugins;
	}

}

/* Begin customization */

if( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
	$_pluginsToAutoDisable = array(
		'w3-total-cache/w3-total-cache.php',
		'wordpress-seo/wp-seo.php',
		'google-analytics-for-wordpress/googleanalytics.php',
	);
	new CWS_Disable_Plugins_When_Local_Dev( $_pluginsToAutoDisable );
}[/php]

Revisions

Revision Differences

December 18, 2012 @ 13:49:06Current Revision
Content
 Added: <pre>Look up Mark Jaquith for more info.
 Added: [php]&lt;?php
 Added: /*
 Added: Plugin Name: Disable plugins when doing local dev
 Added: Description: If the WP_LOCAL_DEV constant is true, disables plugins that you specify
 Added: Version: 0.1
 Added: License: GPL version 2 or any later version
 Added: Author: Mark Jaquith
 Added: Author URI: http://coveredwebservices.com/
 Added: */
 Added: class CWS_Disable_Plugins_ When_Local_Dev {
 Added: static $instance;
 Added: private $disabled = array( );
 Added: /**
 Added: * Sets up the options filter, and optionally handles an array of plugins to disable
 Added: * @param array $disables Optional array of plugin filenames to disable
 Added: */
 Added: public function __construct( Array $disables = NULL ) {
 Added: // Handle what was passed in
 Added: if( is_array( $disables ) ) {
 Added: foreach( $disables as $disable )
 Added: $this-&gt;disable( $disable );
 Added: }
 Added: // Add the filter to both single site and multisite plugin lists.
 Added: add_filter( 'option_active_plugins', array( $this, 'do_disabling' ) );
 Added: add_filter( 'site_option_ active_sitewide_plugins', array( $this, 'do_disabling' ) );
 Added: // Allow other plugins to access this instance
 Added: self::$instance = $this;
 Added: }
 Added: /**
 Added: * Adds a filename to the list of plugins to disable
 Added: */
 Added: public function disable( $file ) {
 Added: $this-&gt;disabled[] = $file;
 Added: }
 Added: /**
 Added: * Hooks in to the option_active_plugins and site_option_active_ sitewide_plugins
 Added: * filter and does the disabling
 Added: * @param array $plugins WP-provided list of plugin filenames
 Added: * @return array The filtered array of plugin filenames
 Added: */
 Added: public function do_disabling( $plugins ) {
 Added: if( count( $this-&gt;disabled ) ) {
 Added: foreach( (array)$this- &gt;disabled as $plugin ) {
 Added: if( current_filter() == 'option_active_plugins' )
 Added: $key = array_search( $plugin, $plugins );
 Added: else
 Added: $key = !empty( $plugins[$plugin] ) ? $plugin : false;
 Added: if( false !== $key )
 Added: unset( $plugins[$key] );
 Added: }
 Added: }
 Added: return $plugins;
 Added: }
Deleted: Added: }
 Added: /* Begin customization */
 Added: if( defined( 'WP_LOCAL_DEV' ) &amp;&amp; WP_LOCAL_DEV ) {
 Added: $_pluginsToAutoDisable = array(
 Added: 'w3-total-cache/ w3-total-cache.php',
 Added: 'wordpress-seo/ wp-seo.php',
 Added: 'google-analytics- for-wordpress/ googleanalytics.php',
 Added: );
 Added: new CWS_Disable_Plugins_ When_Local_Dev( $_pluginsToAutoDisable );
 Added: }[/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags:

No comments yet.

Leave a Reply