tags:

views:

24

answers:

1

The function below is supposed to fire when the theme it belongs to is activated. However, to get it to actually create the categories inside the conditional, I'm having to (1) activate the theme, (2) Activate any other theme (3) Activate the theme again

What gives, it should process upon the first time its activated.

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

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

        wp_create_category('nofollow');
        $myCategory1['cat_ID'] = get_cat_id('nofollow');
        $myCategory1['category_parent'] = 1;
        wp_update_category($myCategory1);

        wp_create_category('noindex');
        $myCategory2['cat_ID'] = get_cat_id('noindex');
        $myCategory2['category_parent'] = 1;
        wp_update_category($myCategory2);

    }
}
A: 

I answered this in your other question, then saw it here. Hopefully the same answer applies and works with your re-activation conundrum. Try using this hook instead of your $_GET request, and maybe you need to make this a plugin instead of putting it in your functions.php file to get it to run when the theme is activated. Previous answer follows:

You'll want to use an action hook. Specifically, 'switch_theme'. This is the codex page for all action hooks, I can't link to switch_theme specifically, but scroll down and you'll find it. There is no specific information on this hook, but usage is simple. You can include your function in functions.php or in a plugin file, and after the function definition, include this hook:

function add_my_categories($my-theme-name){
        //if $my-theme-name == 'my_theme_name
            //test if category exists
            //if exists, update
            //if doesn't exist, create and assign parent
    }
add_action('switch_theme','add_my_categories');

the 'add_action()' call will run the named function when the named hook is encountered in wordpress. The 'switch_theme' hook runs after a theme is changed.

It's important to know that this hook will provide the name of the new current theme to your function, which can accept it as an argument if you need it. For instance, to make sure the function only runs if your theme is the one activated. I suppose if this function is in your theme's functions.php file, it will NEVER run unless your theme is activated, so you can determine if you need to double check the theme name.

kevtrout