views:

28

answers:

1

I have a specific bit of setup code for my theme that I only want to process when the theme is first activated. I was under the impression that I could use register_activation_hook for this, but it does not appear to be working.

Example...

In my functions.php file, I want the doThemeSetup() function to run only upon theme activation and no other time...

function doThemeSetup(){
  //stuff here only runs once, when theme is activated
}

register_activation_hook(__FILE__, 'doThemeSetup');

UPDATE: Since posting this question, I have found that register_activation_hook is only available to plugins and not themes.

I have found a means to do similar action in themes but I'm getting inconsistent results:

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) 
{
  //do something
}

The code above does not seem to run when the theme is first activated, but rather when the theme is switched to from another theme.

A: 

This is more of a workaround than solution, but if everything other fails you can try it:

<?php
if (get_option('themename_installed') != 'true'){
    if (doThemeSetup()){
        add_option('themename_installed','true');
    }

}
?>

This way the doThemeSetup is ran only once.

Smaug