1

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.

  • This seems a little backwards, the problem is "when the plugin is active but the theme is different, deactivate the plugin", so should the check not be in the plugin? Otherwise, all the user has to do is reactivate the plugin and we're back to square 1? – Tom J Nowell Jun 2 '18 at 19:14
  • Please remember, this should be optional. No playing with user's choices unless they're optional, especially these that can relate to other parts that are not yours. – coolpasta Jun 2 '18 at 23:33
  • This does not affect users choices. The plugin only provides additional options into the corresponding theme's customizer, so if and when they decide not to use that particular theme, they are not losing anything pertaining to their site or other plugins. @TomJNowell - if they did reactivate the plugin but not the theme, then all they have is an active plugin that doesn't do anything, no options nothing, it will just site there showing that its active. – tsquez Jun 3 '18 at 3:11
1

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 );
  • Hi there, thanks for answering so fast, unfortunately this didn't work, even when I tried adding include_once(ABSPATH.'wp-admin/includes/plugin.php');. I also tried a few variations but nothing. I still appreciate it very much. – tsquez Jun 2 '18 at 19:03
  • 2
    I believe that the correct action hook for your needs would be switch_theme, providing that your code is in the DahTheme. You can see the Codex for more information. codex.wordpress.org/Plugin_API/Action_Reference/switch_theme – Steve Jun 2 '18 at 19:07
  • Hi there yes the code is in DahTheme and you are correct sir. I went to the link to the Codex and used the example code, but I modified it a little. I used this: function dahtheme_setup_options () { if ( is_plugin_active('dahplugin/dahplugin.php') ) { deactivate_plugins('dahplugin/dahplugin.php'); } } add_action('switch_theme', 'dahtheme_setup_options'); and it worked. Thank you all for helping, it's greatly appreciated. – tsquez Jun 2 '18 at 19:24
  • Within your plugin you should probably also check to see if the DahTheme is active and deactivate it if not. Basically like Tom suggests in another answer. – Steve Jun 2 '18 at 19:43
1

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

  • Basically, the free theme I created offers a few customizer options. The premium plugin I created for it adds additional customizer options when activated. So if the theme is not active, there is no need for the plugin to be active for it. If they reactivate the theme, they can go back in and reactivate the plugin as well. So the option I was pointed to earlier works great. Still gonna try yours and see what happens though. Big thank you. – tsquez Jun 2 '18 at 19:39
  • So I have another question that was just asked to me...what about Child Themes? If the user switches to a child theme of DahTheme then the plugin shouldn't be deactivated. Never thought of this really. I was thinking, well the user would just have to go back in to reactivate the plugin, but the person who asked me the question said the same thing as @coolpasta stated "No playing with user's choices unless they're optional" How would we take into account Child Themes? – tsquez Jun 10 '18 at 13:52
  • That problem has no solution so long as you're using a theme based check – Tom J Nowell Jun 10 '18 at 20:14
  • You are correct, so I looked into this entire issue with a little more thought and decided to go with is_plugin_active instead. This seems to do exactly what I want. I was loading additional panels and sections for the customizer when the plugin was being activated. as I said before now I am using the is_plugin_active to load those panels and sections and it works beautimous. Now the user can switch themes even use a child theme and it works as it should. I just want to thank everyone who helped and offered suggestions. I learned a lot with this little endeavor. Until Next time. – tsquez Jun 11 '18 at 13:43
  • Can you write that as a separate answer? Nobody can give you rep for upvoting a comment, and you can mark your own answer as correct, and it's easier to find for others with the same question – Tom J Nowell Jun 11 '18 at 15:32
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.