How to mass-insert categories?

This is main function of the import process.

function menu_recursive_output( $branch_objet, $parent_id, $level ) {

// JUST FOR COUNTING ITEMS
static $counter = 1;

if ( $level ) {

    // CHECK IF ITEM EXISTS ALREADY
    // IF SO, THIS WILL RETURN ARRAY CONTAINING ID OF EXISTING ITEM
    $created_cat = term_exists( $branch_objet->name, 'category', $parent_id);   

    // IF NOT, WE CREATE ITEMS
    // WITH 
    if ( ! $created_cat ) {

    $created_cat = wp_insert_term( $branch_objet->name, 'category', array(
        'description'=> $parent_id,
        'slug'=> $branch_objet->id . '_' . $branch_objet->slug,
        'parent'=> $parent_id
    ));

    } else {

    $created_cat = wp_update_term( $created_cat['term_id'], 'category', array(
        'description'=> $parent_id . 'updated!',
        'slug'=> $branch_objet->id . '_' . $branch_objet->slug,
        'parent'=> $parent_id
    ));

    }
}
//if ( is_wp_error( $created_cat ) ) echo $created_cat->get_error_message();
// ECHOING DATA JUST TO VISUAL CHECK
echo $counter; $counter++;
for ( $i = 0; $i < $level ; $i++) echo '--';
echo '|';
echo '<a href="' . get_category_link($created_cat['term_id']) . '">' . $branch_objet->name . '</a> | id = ' . $created_cat['term_id'] . ' | parent=" . $parent_id . " | level=" . $level . "<br />';

// $options_categories[ $created_cat['term_id'] ] = $branch_objet->name;

// IF CHILDREN START A NEW RECURSION WITH EACH CHILD
// WITH CATEGORY ID AS PARENT ID
if ( $branch_objet->children != '-' ) {
    foreach( $branch_objet->children as $sub_branch ) {
        menu_recursive_output( $sub_branch, $created_cat['term_id'], $level + 1 );
    }
}

}

First I need a object that contains the category. In this case, it should be a object, but could be easily adapted to deal with an array.

I start the process with providing the object and start a level 0 :

// START RECURSION 
menu_recursive_output ( $cat_object, 0, 0 );

You could easily change it create a custom taxonomy by changing 'category' in wp_insert_termand wp_update_term functions.

In my case, I had for each category, and ID and a slug provided, because I was importing from another database. I don’t know your specific, so you may need to adapt it.

Hope this could help, feel free to ask and comment.

EDIT TO ADD CONTEXT INFORMATION :

I had used this function in a template file, instead of the regular loop, so I just lauch the import process by displaying a particular page of my page and I output the data with echo so I have an instant and clear view of the structure of my categories, see if it looks OK.

As you see, the function also could update data so if you change something in your categories file (name for example), you just need to run it again and data will be update.

What it does NOT do is modifying structure : if you delete a category from the imported file, it will not be deleted on the WP structure. It does not compare data, just parse it, and for each item create it if not present, update it if present.

To start testing, I just changed one line :

if ( $level ) {

by

if ( $level < 1 ) {

so the import stops right after the first level (parents) categories. If everything’s fine at this point (name, hierarchy) you could remove the conditionnal number.

To import data i just use :

$file_to_import = file_get_contents( TEMPLATEPATH . '/import/arbo.php' );

tech