• 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: https://wordpress.stackexchange.com/questions/4041/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 not very good with code, any help appreciated.

Viewing 1 replies (of 1 total)
  • You can try by activating plugin on theme activation hook.

    `function myactivationfunction($oldname, $oldtheme=false) {
    //write code to activate plugin for that you can use activate_plugins ();

    //activate_plugins ( string|array $plugins, string $redirect = ”, bool $network_wide = false, bool $silent = false )
    }
    add_action(“after_switch_theme”, “myactivationfunction”, 10 , 2);

Viewing 1 replies (of 1 total)
  • The topic ‘Activate plugins by a theme's functions.php’ is closed to new replies.