Here the code I used:
<?php
/*
Plugin Name: City Importer
Plugin URI:
Description: Imports csv-file with states/counties/cities as categories
Version: 0.1
Author: MichaelH & HendrikH
Author URI:
//import categories from csv
//sample data for $import_file category,category parent,slug,description
//Parent categories must be in file before child categories
//
//"county1",""
//"county2",""
//"city1","county1"
//"city2","county2"
//"city3","county1"
//"county3",""
//"County Cork","","county cork","county cork descripiton of this cat"
//"County Morefields","","","This is description of county morefields"
function import_cats();
{
require_once(ABSPATH . "wp-admin/includes/admin.php");
$import_file = 'cat.csv';
$import = array();
$fh = fopen($import_file,'r');
while($t = fgetcsv($fh)) {
$import[] = $t;
}
foreach ($import as $importcat) {
$numfields=count($importcat);
$cat_ID = get_cat_ID($importcat[0]);
$cat_name = $importcat[0];
$category_parent = get_cat_ID($importcat[1]);
if ($numfields > 2)
$category_nicename = sanitize_title($importcat[2]);
if ($numfields > 3)
$category_description = $importcat[3];
$args = compact('cat_ID', 'cat_name', 'category_description', 'category_nicename', 'category_parent');
wp_insert_category($args);
}
register_activation_hook( __FILE__, 'import_cats' );
}
?>
My csv file is placed directly in the plugin folder.