9

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?

-2

You might be looking for the after_setup_theme hook:

http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme

| improve this answer | |
  • can I use this same function to do something after deactivation? The example seems to say so but im curious – TheWebs Feb 16 '13 at 17:11
  • you can play with $_GET['previewed'] and $_GET['theme'] – birgire Feb 16 '13 at 17:20
  • 7
    -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. – Mark Kaplun Feb 16 '13 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 '13 at 17:23
26

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...
}

http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme

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

| improve this answer | |
3

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".

| improve this answer | |
  • Good point @mark-kaplun – wesamly Oct 16 '16 at 12:26
2

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.

Example:

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

        // RUN THEME_ACTIVATION STUFF HERE

    }
}

Hope this helps.

| improve this answer | |
  • 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 – Gerard Reches May 26 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 SEO yesterday
  • Sure, it makes it easier – Gerard Reches yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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