after_setup_theme always runs

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 
    echo '<script type="text/javascript">alert("This action was run")</script>';

Any insight would be much appreciated!

Solutions Collecting From Web of "after_setup_theme always runs"

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:

Here is my modified code

add_action( 'after_switch_theme', 'fwp_theme_setup' );
function fwp_theme_setup(){ 

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.

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

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.

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