disable active plugins for specific theme

I want to disable some plugins from a specific theme.I am using deactivate_plugins hook to deactivate. following is my code.

add_action('wp_head','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 == 'Twenty Sixteen'){
        if ( is_plugin_active('press-release/init.php') ) {
            deactivate_plugins('press-release/init.php');    
        }
    }
}

This code deactivate plugins at wp_head hook but issue is I want active plugins to disable only when theme is Twenty Sixteen while wants to keep enable or disable as it was already on other themes.

But my code deactivates plugins for all themes. :(

Other solution was of deregistering css and js files of each plugin but its tough to find all css and js files for all plugins and it will also deregister for all themes while I want active plugins to disable on specific theme.

Please help me if you can in this case. Thanks.

Topic deactivation hooks plugins themes Wordpress

Category Web


What @Sumit says in the comments (and you found out for yourself) is correct. If you deactivate a plugin in a theme it is deactivated permanently, until it is activated again.

Also, if you attach your deactivation action to wp_head it is executed at every pageload, which is not necessary. What you need to do is deactivate the plugin when the theme is activated, and reactivate the plugin when another theme is activated. There are action hooks for this: after_switch_theme on activation and switch_theme on deactivation. So you would have:

add_action('after_switch_theme','disable_plugins');
add_action('switch_theme','enable_plugins');

You already have the disable_plugins function, the other one you'll need to write yourself.

About

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