4

I'm trying to insert a term for a custom taxonomy and a custom post type in Wordpress. The problem I'm running in to is that it successfully inserts a term, and at the right hierarchy level, but the term has no name. The line that inserts the term is:

$lasttermarray = wp_insert_term($categoryname, "department", array( "parent" => $parentcategory ) );

I've checked to make sure that both $categoryname and $parentcategory are set to the right values. Like I said, the term is being inserted, just with a blank name. The slug of the blank term is being set to the ID of the new term.

Is there any reason why the function would not be reading the name variable and causing it to do this? Thanks for your help.

3
  • What is return from the function precisely?
    – Rarst
    Jun 14, 2013 at 20:43
  • This is a serialized version of the array it's returning: a:2:{s:7:"term_id";i:57;s:16:"term_taxonomy_id";i:57;}
    – Eric Strom
    Jun 14, 2013 at 20:46
  • Found out the problem. The term that I was trying to add included a special character. Apparently the function didn't error out, it just couldn't add the name so it added a blank string instead.
    – Eric Strom
    Jun 14, 2013 at 21:21

1 Answer 1

1

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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.