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!
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 usesswitch_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
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.Similarly add a callback to
switch_theme
will only be called when your theme is de-activated.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;