WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The code below is near the top of my functions.php and is set to run only when my theme is activated:

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

However, even though its creating the category "my-utilities", it is apparently not able to set the parent of the category1 and category2 items to the newly created category.

Perhaps its too soon to call get_cat_id on the newly created category?

I believe it has something to do with that, since the category's reparent on the 2nd time the theme is activated. This, I presume, is since the category that will be used as parent has been created previously, the routine has no problem finding its ID and using it as the parent for category1 and category2.

What am I missing?

// with activate make sure theme's utility categories are present and parented correctly
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {

    if (file_exists(ABSPATH.'/wp-admin/includes/taxonomy.php'))


        //find out the ID of the newly created category, "my-utilities"
        $my_default_cat = my_cat();


        //if the categories already existed, reparent them
        $myCategory1['cat_ID'] = get_cat_id('category1');
        $myCategory1['category_parent'] = $my_default_cat;

        $myCategory2['cat_ID'] = get_cat_id('category2');
        $myCategory2['category_parent'] = $my_default_cat;


//utility category
function my_cat()
        return get_cat_ID('my-utilities');
        if(term_exists(1)) return "1";
        else return get_option('default_category');
share|improve this question
up vote 3 down vote accepted

You're looking for this:

register_activation_hook( __FILE__, 'your_plugin_activate_function_name' );

Edit: for a theme, you can use something like this instead:

$theme_version = get_option('my_theme_version');

switch ((string) $theme_version) {
  // an early alpha... run upgrade code
  case '0.1':

  // another version... run upgrade code
  case '0.5':

  // add other cases as needed... without any break statement.

    // if we're here and in the admin area (to avoid race conditions),
    // actually save whichever changes we did
    if (is_admin()) {
      update_option('my_theme_version', '1.0');
      // do other stuff

  // we're at the latest version: bail here.
  case '1.0':

    // install code goes here
share|improve this answer
this is a theme. I wasn't aware that hook was available when a theme is activated. Confirm? – Scott B Feb 7 '11 at 19:42
Ah, sorry, I misunderstood. You're out of luck then: core.trac.wordpress.org/ticket/7795. Personally, I use a db setting to know which version of my theme is currently installed on each page load, and install/upgrade it as needed based on that. – Denis de Bernardy Feb 7 '11 at 19:45
I've updated my reply so you get an idea of what I'm meaning by using an option. – Denis de Bernardy Feb 7 '11 at 19:53
great suggestion. Works even on theme activation without me having to even go to theme options. Thanks! – Scott B Feb 7 '11 at 20:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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