13

I am setting up a child theme for some of my faculty members, and as a part of the theme, I would like a handful of plugins to be activated at the time that the theme is activated. So, naturally, I used the after_setup_theme action and called my setup function. It works great, except it runs on EVERY request (admin and otherwise). I proved this by adding this to the end of the setup function:

echo '<script type="text/javascript">alert("This action was run")</script>';

And as a result get a javascript alert on every admin request and every front-end request (I have a network setup, so obviously on sites where this theme is not active, it's not running the function)

So the question is, is this a bug? Am I somehow doing something wrong? Here is the complete code that I am using:

add_action( 'after_setup_theme', 'fwp_setup' );
function fwp_setup(){
    // -- Unrelated code remove for the sake of brevity 
    require_once($_SERVER['DOCUMENT_ROOT'].'/wp-admin/includes/plugin.php');
    activate_plugin('enable-media-replace/enable-media-replace.php');
    activate_plugin('seo-image/seo-friendly-images.php');
    activate_plugin('w3-total-cache/w3-total-cache.php');
    echo '<script type="text/javascript">alert("This action was run")</script>';
}

Any insight would be much appreciated!

3
  • SOLUTION: after_switch_theme does exactly what I intended here. It fires after the theme is switched TO your theme. One of the solutions mentioned below uses switch_theme. This does not have the desired results, since it only happens upon switching away from your theme. Reference this Trac comment Apr 26, 2012 at 13:03
  • please post that as an answer, rather than as a comment to your question. :) Apr 26, 2012 at 13:31
  • 2
    Apologies. It would not let me do that yet. I will add it as an answer in a bit. Apr 26, 2012 at 19:52

5 Answers 5

10

SOLUTION: after_switch_theme does exactly what I intended here. It fires after the theme is switched TO your theme. One of the solutions mentioned below uses switch_theme. This does not have the desired results, since it only happens upon switching away from your theme.

Here is an article that I found as reference: http://core.trac.wordpress.org/ticket/7795#comment:29

Here is my modified code

add_action( 'after_switch_theme', 'fwp_theme_setup' );
function fwp_theme_setup(){ 
    require_once($_SERVER['DOCUMENT_ROOT'].'/wp-admin/includes/plugin.php');
    activate_plugin('enable-media-replace/enable-media-replace.php');
    activate_plugin('seo-image/seo-friendly-images.php');
    activate_plugin('w3-total-cache/w3-total-cache.php');
}
6

The after_setup_theme action is intended to fire on every WordPress load. It is simply part of the process during which WordPress invokes the template system, determines the various setup parameters for the Theme, and then proceeds on with subsequent processing, such as determining the correct template to display, etc.

In other words, the after_setup_theme represents the point at which WordPress sets up the current Theme, not the point at which the administrator activates and/or configures the current Theme.

What you're looking for is a Theme activation hook, which currently isn't available, but is under consideration/development.

3
  • The Codex entry link is terribly misleading then. It says "This hook is called during a themes initialization." That means, to me, that the hook should be called when it's activated. I understand what you're saying though. Terribly misleading. Apr 26, 2012 at 12:36
  • 5
    init !== activation. Normally you call __construct() function in php also an init function. That means: Initialize the instance.
    – kaiser
    Apr 26, 2012 at 12:51
  • The trac ticket is 7 years old and seems won't be fixed!
    – numediaweb
    Jun 7, 2015 at 19:16
4

Unfortunately there is no theme-activation hook. However, a this question does provide a work-around for that.

Simply use the 'theme activation hook' to activate the plug-ins.

A better solution, along the same vein is this one. Both essentially use the switch_theme hook.


As per the OP comments and the linked trac ticket - after_switch_theme is the hook required.

This passes the old theme's name as an argument. However, if this is in your functions.php (which it should be...) the callback will only fire when your theme is being activated.

add_action( 'after_switch_theme', 'wpse50298_setup' );
function wpse50298_setup($theme_switching_from){
    // Your theme is being activated
}

Similarly add a callback to switch_theme will only be called when your theme is de-activated.

add_action( 'switch_theme', 'wpse50298_deactivate' );
function wpse50298_deactivate($theme_switching_to){
    // Your theme is being deactivated
}
4
  • Neither of these options worked for me. switch_theme only worked after deactivting the theme. However, after_switch_theme worked perfectly. Apr 26, 2012 at 13:01
  • 1
    updated answer :) May 10, 2012 at 11:24
  • switch_theme doesn't seem to work anymore. I think it is because of the changes in 3.4 activation. At least it isn't working for me.
    – Jake
    Aug 3, 2012 at 0:18
  • after_switch_theme works perfect. This should be the accepted answer. I am using WP 3.5
    – Ardee Aram
    Jan 25, 2013 at 14:45
0

A better solution might be to use Thomas Griffin's plugin activation script. This will prompt users to install plugins of your choice when the theme is in use. I think this is a great way to separate the plugin from the theme, and still get the use of it.

Within your theme, check if the plugin is active before using it's functions. This will allow users more choice and control.

You can set up the activation script easily with Knapsack.

1
0

Your best fix would be now to use switch_theme hook and filter the passed '$theme' argument to see if it is the current them then if not return;

function nw_update_network($theme) {
   if ($theme !== 'my_theme_name') return;

   // Your code here
}

add_action('switch_theme', 'nw_update_network');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.