wp_insert_term is adding a term that has no name

As noted in the OP’s comments, some non-ASCII characters may not be supported in term names under certain conditions. To replace all suspect characters with an underscore, use the following code:

$categoryname = preg_replace('/[^a-z0-9]/i', '_', strtolower($categoryname));
wp_insert_term($categoryname, "department");

To replace all suspect characters with a dash, use the following code:

$categoryname = preg_replace('/[^a-z0-9]/i', '-', strtolower($categoryname));
wp_insert_term($categoryname, "department");

To simply remove all suspect characters, use the following code:

$categoryname = preg_replace('/[^a-z0-9]/i', '', strtolower($categoryname));
wp_insert_term($categoryname, "department");

Leave a Comment