I want to be able to create categories in bulk, but for some reason, they are not showing up in the manage category section, it only shows the parent, but when you write a post the children show up fine.
the format is as follows
Finance/Mutual Funds
Finance/Personal Finance
Finance/Real Estate
<?php
add_action('admin_menu', 'bulk_cat_menu');
function bulk_cat_menu() {
add_management_page('Category Import', 'Category Import', 10, __FILE__, 'bulk_add_cat');
}
function bulk_add_cat() {
global $wpdb;
$cats = $_POST['bulk_category_list'];
$s = split("\n", $cats);
foreach($s as $cat){
$cat = trim(preg_replace("![\r\n]+!", '', $cat));
$cats = split('\/', $cat);
$last_id = 0;
foreach($cats as $c2){
$last_id = wp_create_category($c2, $last_id);
}
}
?>
<div class="wrap nosubsub">
<div class="icon32" id="icon-edit"><br/></div>
<h2>Bulk Category Import</h2>
<form name="bulk_categories" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post">
<br/>
<strong>Enter a list of category names, one per line:</strong>
<br/>
<textarea name="bulk_category_list" rows="20" style="width: 99%;"></textarea>
<br/>
<p class="submit"><input type="submit" name="submit" value="Add categories"/></p>
</form>
</div>
<?php
}
?>
Monster Coder answers:
hello
try with this
function get_parent_id($catname){
$cat= get_term_by('name', $catname, 'category');
//print_r($cat);
if(!empty($cat->term_id)) return $cat->term_id;
if(empty($cat->term_id)){
//create category
$cat = wp_insert_term($catname, 'category');
}
return get_parent_id($catname);
}
if(isset($_POST['submit'])){
$lines = explode(PHP_EOL,$_POST['bulk_category_list']);
foreach($lines as $line){
$split_line = explode('/',$line);
//get the parent category
$paren_id = get_parent_id($split_line[0]);
$subcat_id = wp_create_category($split_line[1],$paren_id);
echo "Category: {$split_line[1]} is created with ID: {$subcat_id} <br/>".PHP_EOL;
}
}
full code
(i did this in a file at wordpress root):
require('wp-config.php');
require_once(ABSPATH.'wp-admin/includes/taxonomy.php');
function get_parent_id($catname){
$cat= get_term_by('name', $catname, 'category');
//print_r($cat);
if(!empty($cat->term_id)) return $cat->term_id;
if(empty($cat->term_id)){
//create category
$cat = wp_insert_term($catname, 'category');
}
return get_parent_id($catname);
}
if(isset($_POST['submit'])){
$lines = explode(PHP_EOL,$_POST['bulk_category_list']);
foreach($lines as $line){
$split_line = explode('/',$line);
//get the parent category
$paren_id = get_parent_id($split_line[0]);
$subcat_id = wp_create_category($split_line[1],$paren_id);
echo "Category: {$split_line[1]} is created with ID: {$subcat_id} <br/>".PHP_EOL;
}
}
?>
<form name="bulk_categories" action="" method="post">
<br/>
Enter a list of category names, one per line:
<br/>
<textarea name="bulk_category_list" rows="20" style="width: 99%;"></textarea>
<br/>
<p class="submit"><input type="submit" name="submit" value="Add categories"/></p>
</form>
Monster Coder comments:
here is simpler version.
if(isset($_POST['submit'])){
$lines = explode(PHP_EOL,$_POST['bulk_category_list']);
foreach($lines as $line){
$split_line = explode('/',$line);
//insert/get the parent category.
$parent_id = wp_create_category($split_line[0]);
echo "Parent: ".$parent_id;
$subcat_id = wp_create_category($split_line[1],$parent_id);
echo " ...Category: {$split_line[1]} is created with ID: {$subcat_id} <br/>".PHP_EOL;
}
}
I also noticed that sometime child categories are not shown. When I edit any (ANY) category, everything shows correct. may be cache issue. however, the subcategory is listed fine at that moment on the Parent drop down box at Add New Category panel.
chrismccoy comments:
it works, just like the code i posted, but i want them to show up in the category list without updating the parent category, like you said it might be a cache issue?
Monster Coder comments:
When you create posts, these newly categories (with children) will be displayed fine.
for example use these codes:
if(isset($_POST['submit'])){
$lines = explode(PHP_EOL,$_POST['bulk_category_list']);
foreach($lines as $line){
$split_line = explode('/',$line);
//insert/get the parent category.
$parent_id = wp_create_category($split_line[0]);
echo "Parent: ".$parent_id;
$subcat_id = wp_create_category($split_line[1],$parent_id);
echo " ...Category: {$split_line[1]} is created with ID: {$subcat_id} <br/>".PHP_EOL;
}
wp_dropdown_categories('hierarchical=true&hide_empty=0');
}
Baki Goxhaj answers:
You are saying: <strong>$last_id = 0;</strong> - Does your Finance category really has a 0 ID?
I saw that on WP codex page of wp_create_category they give a sample with <strong>Uncategorized</strong> cat ID as 0, but from my experience <strong>Uncategorized</strong> has always an <strong>ID of 1</strong>. So, try to assign the proper ID to <strong>$last_id</strong> variable.
chrismccoy comments:
how would i go about assigning the category id, since it hasnt been created. its a cut and paste with a list of cats sub cats
Baki Goxhaj comments:
The category ID you assign is not for the categories themselves, it's for the parent one. If you just want to create parent categories, remove the $last_id altogether.
chrismccoy comments:
ya i want parent and child
so
Finance/Loans
finance would be the parent, and Loans would be the child
Baki Goxhaj comments:
Is finance already created?
chrismccoy comments:
no it isnt created,
Finance/Loans
means create finance, with subcat Loans
Baki Goxhaj comments:
Here goes a neater version. This code produces Category with subcategories. But the problem persists. They show fine in the post editor as cat->sub. But in categories, you need to add another category and refresh for them to get the relationship right. It seems like a WordPress bug.
<?php
function bulk_add_cat() {
global $wpdb;
$cats = $_POST['bulk_category_list'];
$s = split("\n", $cats);
// Convert special characters to html entities
$cat = htmlspecialchars( $s[0], ENT_QUOTES);
// Create first cat as parent and get ID
$cat_id = wp_create_category( $cat );
// Remove parent category form the list of children not to delicate
$cats = array_slice( $s, 1 );
foreach( $cats as $c2 ){
$c = htmlspecialchars( $c2, ENT_QUOTES);
wp_create_category( $c, $cat_id );
}
?>
<div class="wrap nosubsub">
<div class="icon32" id="icon-edit"><br/></div>
<h1>Parent Cat Id is: <?php echo $cat_id; ?></h1>
<form name="bulk_categories" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post">
<br/>
Enter a list of category names, one per line:
<br/>
<textarea name="bulk_category_list" rows="20" style="width: 99%;"></textarea>
<br/>
<p class="submit"><input type="submit" name="submit" value="Add categories"/></p>
</form>
</div>
<?php
} ?>