Wordpress - WordPress Theme activation hook?

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


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


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.

Tags:

Themes

Init