Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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 –  Aaron Wagner Apr 26 '12 at 13:03
    
please post that as an answer, rather than as a comment to your question. :) –  Chip Bennett Apr 26 '12 at 13:31
2  
Apologies. It would not let me do that yet. I will add it as an answer in a bit. –  Aaron Wagner Apr 26 '12 at 19:52

4 Answers 4

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');
}
share|improve this answer

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.

share|improve this answer
    
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. –  Aaron Wagner Apr 26 '12 at 12:36
4  
init !== activation. Normally you call __construct() function in php also an init function. That means: Initialize the instance. –  kaiser Apr 26 '12 at 12:51

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
}
share|improve this answer
    
Neither of these options worked for me. switch_theme only worked after deactivting the theme. However, after_switch_theme worked perfectly. –  Aaron Wagner Apr 26 '12 at 13:01
1  
updated answer :) –  Stephen Harris May 10 '12 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 '12 at 0:18
    
after_switch_theme works perfect. This should be the accepted answer. I am using WP 3.5 –  Ardee Aram Jan 25 '13 at 14:45

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.

share|improve this answer
2  
An answer should be more than just a link to an external site. Please add a solution. –  toscho May 5 '12 at 2:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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