admin管理员组文章数量:1406344
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.
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.
Share Improve this question edited Jun 2, 2018 at 18:50 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jun 2, 2018 at 18:30 tsqueztsquez 851 silver badge9 bronze badges 3- 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 ♦ Commented Jun 2, 2018 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 Commented Jun 2, 2018 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 Commented Jun 3, 2018 at 3:11
4 Answers
Reset to default 1I 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 );
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 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 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
};
本文标签: Deactivate Plugin on Theme Switch
版权声明:本文标题:Deactivate Plugin on Theme Switch 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745004452a2637174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论