Title: Activate plugins by a theme&#039;s functions.php
Last modified: August 30, 2016

---

# Activate plugins by a theme's functions.php

 *  [AM77](https://wordpress.org/support/users/andy277/)
 * (@andy277)
 * [11 years ago](https://wordpress.org/support/topic/activate-plugins-by-a-themes-functionsphp/)
 * 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: [http://wordpress.stackexchange.com/questions/4041/how-to-activate-plugins-via-code](http://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)

 *  [WisdmLabs](https://wordpress.org/support/users/wisdmlabs/)
 * (@wisdmlabs)
 * [11 years ago](https://wordpress.org/support/topic/activate-plugins-by-a-themes-functionsphp/#post-6416757)
 * 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.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [WisdmLabs](https://wordpress.org/support/users/wisdmlabs/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/activate-plugins-by-a-themes-functionsphp/#post-6416757)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
