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(){
$current_theme = wp_get_theme();
$current_theme_name = $current_theme->Name;

if($current_theme_name == ‘DahTheme’){
if ( is_plugin_active(‘dahplugin/dahplugin.php’) ) {

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.

Read more here:: Deactivate Plugin on Theme Switch

Leave a Reply

Your email address will not be published. Required fields are marked *