Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gustavobcx/63ff71c2e8b76d7181f287fb858ddecc to your computer and use it in GitHub Desktop.
Save gustavobcx/63ff71c2e8b76d7181f287fb858ddecc to your computer and use it in GitHub Desktop.

Revisions

  1. @johnpbloch johnpbloch revised this gist Feb 11, 2012. 1 changed file with 32 additions and 23 deletions.
    55 changes: 32 additions & 23 deletions disable-plugins-when-doing-local-dev.php
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,33 @@
    <?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/
    */
    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();
    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) {
    public function __construct( Array $disables = NULL ) {
    // Handle what was passed in
    if ( is_array( $disables ) ) {
    foreach ( $disables as $disable )
    if( is_array( $disables ) ) {
    foreach( $disables as $disable )
    $this->disable( $disable );
    }

    // Add the filter
    // 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;
    @@ -38,28 +41,34 @@ public function disable( $file ) {
    }

    /**
    * Hooks in to the option_active_plugins filter and does the disabling
    * 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 ) {
    $key = array_search( $plugin, $plugins );
    if ( false !== $key )
    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 ) {
    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' );
    */
    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 );
    }
  2. @markjaquith markjaquith created this gist Jun 24, 2011.
    65 changes: 65 additions & 0 deletions disable-plugins-when-doing-local-dev.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    <?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' );
    */
    }