Is it possible to activate plugins per theme?
I have a multisite, and I know it is possible to activate plugins per site. But I need plugins to be enabled for specific themes.
Someone asked a similar question - if it is possible to activate plugins by other plugins: How To Activate Plugins via Code?
This was an accepted answer:
//Activate a plugin programmatically - Akismet example
function run_activate_plugin( $plugin ) {
$current = get_option( 'active_plugins' );
$plugin = plugin_basename( trim( $plugin ) );
if ( !in_array( $plugin, $current ) ) {
$current[] = $plugin;
sort( $current );
do_action( 'activate_plugin', trim( $plugin ) );
update_option( 'active_plugins', $current );
do_action( 'activate_' . trim( $plugin ) );
do_action( 'activated_plugin', trim( $plugin) );
}
return null;
}
run_activate_plugin( 'akismet/akismet.php' );
However someone commented about this not being very safe and I'm also not sure if this would be a good idea to add to a theme's functions.php file.
I added this above code to a themes functions.php to activate some plugins and so far this is working without any problems. Would I run into problems with this, or is there some better code to use? There is also this function activate_plugin()
not sure if that function is a better way to activate plugins.
I am no good with code, any help appreciated.