Site icon WP Smith

Deactivate Your Plugin Extension with the Extended Plugin

In the previous post, Extending or Modifying a Plugin, in this two part series, I wrote about modifying by extending an existing plugin using a plugin. This left us with the following question:

How do I deactivate my plugin if the extended plugin was deactivated?

This snippet will allow you to deactivate your extended plugin when the extended plugin deactivates.

<?php
add_action( 'update_option_active_sitewide_plugins', 'dpse_deactivate_self', 10, 2 );
add_action( 'update_option_active_plugins', 'dpse_deactivate_self', 10, 2 );
/**
* Deactivate ourself if Addthis is deactivated.
*/
function dpse_deactivate_self( $plugin, $network_deactivating ) {
if ( !is_plugin_active( 'addthis/addthis_social_widget.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
}

Picking up from the previous post, in the example below, there is a code snippet to customize Display Posts Shortcode. So, if the client decides to deactivate Display Posts Shortcode, then this plugin will deactivate itself automagically.

Yes, it is strange that I am hooking into the updated_option hook. One would think that this could be done with the deactivated_plugin hook, but it cannot since at the time of this post either the documentation (ticket #27189) is wrong or the action occurs at the wrong place (ticket #27189, see also #20241).

Full Extended Plugin

<?php
/**
* Display Posts Shortcode Extension Plugin
*
* @package DT_Display_Posts
* @author Travis Smith <t@wpsmith.net>
* @license GPL-2.0+
* @link http://wpsmith.net
* @link http://wordpress.org/plugins/display-posts-shortcode/
* @copyright 2014 Travis Smith
*
* @wordpress-plugin
* Plugin Name: Display Posts Shortcode - My Extension
* Plugin URI: http://www.wpsmith.net
* Description: Extends Display Posts Shortcode
* Version: 1.0.0
* Author: Travis Smith
* Author URI: http://www.wpsmith.net
* Text Domain: displayposts-ext
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
add_filter( 'display_posts_shortcode_args', 'dpse_shortcode_exclude_posts', 10, 2 );
/**
* Add "not_in" arg to Display Posts Shortcode
*
* @since 1.0.0
*
* @param array $args Default Display Posts Shortcode args.
* @param array $atts Original shortcode attributes.
* @return array $args Modified Display Posts Shortcode args.
*/
function dpse_shortcode_exclude_posts( $args, $atts ) {
if( isset( $atts['not_in'] ) ) {
$args['post__not_in'] = explode( ',', $atts['not_in'] );
}
return $args;
}
add_action( 'update_option_active_sitewide_plugins', 'dpse_deactivate_self', 10, 2 );
add_action( 'update_option_active_plugins', 'dpse_deactivate_self', 10, 2 );
/**
* Deactivate ourself if Addthis is deactivated.
*/
function dpse_deactivate_self( $plugin, $network_deactivating ) {
if ( !is_plugin_active( 'display-posts-shortcode/display-posts-shortcode.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
}