Deactivate Plugin on Theme Switch

I am developing a plugin, let's call it DahPlugin, that provides additional customizer options for a free theme I developed. Let's call this theme DahTheme (I don't want to shamelessly plug either one of them here).

So what I am trying to accomplish is, I would like for DahPlugin to be automatically deactivated the when the user changes themes away from DahTheme.

I found this code snippet here: disable active plugins for specific theme, which works really well if you want to disable plugins if a certain theme is active.

So I decided I would be clever and utilize it in my theme and switch the logic. So I changed the comparison operator from == (equal) to != (not equal)...my thinking: If the current theme name is not equal to "DahTheme", then deactivate "DahPlugin" and here is what it looks like:

if ( ! function_exists('disable_plugins')) :
    function disable_plugins(){
        include_once(ABSPATH.'wp-admin/includes/plugin.php');
        $current_theme = wp_get_theme();
        $current_theme_name = $current_theme-Name;

        if($current_theme_name == 'DahTheme'){
            if ( is_plugin_active('dahplugin/dahplugin.php') ) {
                deactivate_plugins('dahplugin/dahplugin.php');                          
            }
        }
    }
    add_action('after_switch_theme','disable_plugins');
endif;

Aaaaaaand it doesn't work...lol! Of course this happens whenever I think I'm being clever.

So I have been racking my brain and looking all over the place to try and figure out what or why it isn't working and I can't seem to find that either.

Can anyone show me why it's not working and why my line of thinking is wrong. That would be greatly appreciated.

Thanks in advance for any help.

Topic deactivation hooks plugins themes Wordpress

Category Web


This is not exactly the answer to the asked question. However, this will be helpful for many who are looking to perform certain action after activation or deactivation of themes.

// Perform action when your theme is deactivated (actually switched to another)
add_action( 'switch_theme', 'action_switch_theme', 10, 1 ); 
function action_switch_theme( ) { 
    // deactivate your plugin
}; 

// Perform action after your theme is activated (switched from other theme)
add_action( 'after_switch_theme', 'action_after_switch_theme', 10, 1 ); 
function action_after_switch_theme( ) { 
    // activate your plugin
}; 

I basically solved this issue by using is_plugin_active - before I was loading additional panels and sections with the plugin and when someone chose another theme the customize register created by the plugin was still there was still there and when they went into the new themes customizer, a fatal error was generated.

This corrected the issue.


This entire approach is backwards. The problem is not deactivating on theme switch, but making sure the plugin isn't available unless the theme it was built for is active.

So, the check should be in the plugin, not the theme

So lets start by figuring out which theme is active, we can do this via wp_get_theme documented here

We then Register a hook in our plugin on admin_init:

add_action( 'admin_init', function() {
    $theme = wp_get_theme();
} );

Then, if the theme isn't the intended theme, deactivate:

if ( $theme->headers['name'] !== 'DahTheme' ) {
    deactivate_plugins( plugin_basename( __FILE__ ) );
}

Notice that deactivate_plugins is plural, and it requires an absolute path. Because of the use of __FILE__, this check must be in the main plugin file.


Although, this complicates things when switching back to 'DahTheme' as now the plugin is inactive, so it would be better to do the check then return early, so the plugin does nothing, rather than deactivating. This way no customizer options are presented for incompatible themes, and no issues happen when the user previews themes or switches back


I haven't tested this, so it might not work correctly, but the essence of what you're trying to do is hook into the after_switch_theme hook and see if the $old_name is DahTheme. If it is, that means that the current theme isn't DahTheme, so you want to deactivate the plugin.

function wpse_after_switch_theme( $old_name, $old_theme ) {
  if( 'DahTheme' === $old_name ) {
    //* You may or may not need to include wp-admin/includes/plugin.php here
    deactivate_plugins( 'dahplugin/dahplugin.php' );
  }
}
add_action( 'after_switch_theme', 'wpse_after_switch_theme', 10, 2 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.