I know the many tricks to see if said theme is activated, I have scoured the internet. I am not looking for re-pasting of that code. What I am looking for though is weather or not 3.3-3.5 released a new function that does something upon theme_init, I saw some sort of hook, but I can't remember where, in the codex, dealing with doing things after a theme has been initialized..

Any one know?

6 Answers 6


You might be looking for the after_setup_theme hook:


  • can I use this same function to do something after deactivation? The example seems to say so but im curious
    – TheWebs
    Feb 16, 2013 at 17:11
  • you can play with $_GET['previewed'] and $_GET['theme']
    – birgire
    Feb 16, 2013 at 17:20
  • 10
    -1 as this hook is fired at each WP init. It is fired after including the functions.php file of the theme, therefor it is not an "activation" hook. Feb 16, 2013 at 17:20
  • yes true, but the fix for that is mentioned on the Codex page, to check for the above GET parameters or $_GET['activated']
    – birgire
    Feb 16, 2013 at 17:23
  • This is a bad answer Ive posted here long time ago, please avoid it, one cant remove accepted ones, thx
    – birgire
    Mar 4, 2022 at 19:02

For anyone stumbling upon this question; there is an action you can hook into (added 3.3.0) which is fired only on activation of a new theme: after_switch_theme

add_action('after_switch_theme', 'mytheme_setup_options');

function mytheme_setup_options () {
  //doing a thing...


To do something on deactivation of a theme you can use the sister action: switch_theme


With the theme preview features it is unlikely that there will ever be a theme activation hook since themes need to work even without being "activated".

  • Good point @mark-kaplun
    – wesamly
    Oct 16, 2016 at 12:26

After trying @sleepingkiwi method i encountered a problem. A client might try a different theme (even if just for a moment), this might create a problem due to the fact that the "on theme activation" hook we created ran twice.

The best method is using after_switch_theme in concert with Wordpress "update_option" to save and later check an activation noticethus making this method bullet proof.


add_action('after_switch_theme', 'sgx_activation_hook');
function sgx_activation_hook() {
    if(get_option('SOMEPREFIX_theme_activated') != '1') {

        update_option( 'SOMEPREFIX_theme_activated', '1' );



Hope this helps.

  • That shouldn't be the case, the codex specifies that it only triggers for the current theme: Theme functions attached to this hook are only triggered in the theme (and/or child theme) being activated. codex May 26, 2020 at 22:57
  • But that's good, isn't it? we also need to do some cleanup after theme is turned off - but in general, I would think that it is good since its a theme related thingy
    – Sagive
    May 28, 2020 at 13:56
  • Sure, it makes it easier May 28, 2020 at 23:11

I use this code to create the hooks register_vuewp_theme and unregister_vuewp_theme, so I can attach functions to these hooks easely.

function register_vuewp() {
    $one = get_option('vuewp_theme');
    if (false == $one) {
        update_option('vuewp_theme', 'on');
add_action('admin_init', 'register_vuewp');

function unregister_vuewp() {
add_action('switch_theme', 'unregister_vuewp');

The action hook after_switch_theme sometimes may fail because it fires only if the old theme still exists. This is written in the codex.

You can also check it directly in wp-includes/theme.php in the function check_theme_switched() where the action hook is added. You will see that the action hook "after_switch_theme" is inside the condition if ( $old_theme->exists() ) {....}

If you want to be sure the hook fires every time a theme is activated, and no matter if the old theme still exists, you can create your custom hook by hooking the change of the option "stylesheet". Every time a theme is activated that option stores the name of the new active theme. So:

add_action( 'update_option_stylesheet','my_theme_activation_hook',20,3 );
add_action( 'update_site_option_stylesheet','my_theme_activation_hook',20,3 ); //For multisite installations

function my_theme_activation_hook( $old_value, $value, $option ){
    //It adds an action hook after theme activation, no matter if the old theme still exists
    do_action( 'my_after_theme_activation');

Then you can use your action hook "my_after_theme_activation" or whatever you want to call it.

This method is not perfect because a plugin could also update the option "stylesheet" without a real theme activation. I don't see why they should do it, but it's allowed, and you never know. So, I would take that into account. Use it if for you it's not a problem that it fires also if a new theme is not really activated, but one of the plugins just changed the option "stylesheet".

You could also directly hook the change of the option "theme_switched" instead of "stylesheet". Not tested this, but looking at the code it looks like it fires two times when you activate a new theme. The first time to save the old theme stylesheet, and a second time to save the value "false".

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.