Never made this into a plugin and haven’t used this in a whole, but put a csv file cat.txt in your theme folder and put this text in your theme’s index.php (or a page template) and just visit your blog (or page)
<?php
//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"
require_once(ABSPATH . "wp-admin/includes/admin.php");
$import_file = TEMPLATEPATH . '/cat.txt';
$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);
}
?>
Backup your database before attempting WordPress Backups
MichaelH, unfortunately this code is not working. It doesn’t create any categories.
Would it be ok if I populate the tables wp_terms and wp_term_taxonomy by importing other text files?
Text file to import into wp_terms includes the following fields:
term_id, name, slug, term_group.
Text file to import into wp_term_taxonomy includes the following fields:
term_taxonomy_id, term_id, taxonomy, description, parent, count.
It would create categories and save a lot of time. The problem is about if the categories will work properly or if I have to involve other tables.
In theory that could work, though don’t think you need ‘term_id’ in for the terms table or term_taxonomy_id for the term_taxonomy table as those are auto incrementing.