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!

5

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');
}

Leave a Reply

Your email address will not be published. Required fields are marked *