I'm developing a plugin and I've problem with inserting new term to my custom taxonomy.


add_action('init', 'create_tax', 0);
function create_tax()
    $args = array(...);
    register_taxonomy('custom_tax', array('post'), $args);
// I need this function, so I can use it anywhere in my theme & plugin.
function add_new_term($name)
    $result = wp_insert_term($name, 'custom_tax', array('parent'=>0) );
add_new_term('Test Term');

And the result :

object(WP_Error)#159 (2) { ["errors":"WP_Error":private]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data":"WP_Error":private]=> array(0) { } }

So it's mean wp_insert_term executed before register_taxonomy . So I hooked create_tax to after_setup_theme instead of init just for testing. Nothing changed. Finally I've wrapped wp_insert_term() with a function and hooked it to wp_loaded action. But as you can image, it's not possible to put all taxonomy related functions (like wp_insert_term, wp_list_categories ...) to wp_loaded.

And here is the question,

Which hook should I use for register_taxonomy? Because init hook is too late for registering taxonomies.

  • possible duplicate of Inserting a term into a custom taxonomy – Pieter Goosen Mar 31 '15 at 4:38
  • @PieterGoosen actually they are not the same question, because in that question he want to add terms for one time. I mean he want to create a taxonomy and insert 2 default terms to on plugin activation.Because of this, he is using register_activation_hook(). But I'll use wp_insert_term lots of this, for example there will be a button and everytime it clicked I'll add a new term to taxonomy. – Eray Mar 31 '15 at 21:04
  • @PieterGoosen I put my create_tax method to plugin activation function (and disabled&enabled plugin) , but still same error . Here my codes : paste.ubuntu.com/10714681 – Eray Mar 31 '15 at 21:14
  • register_taxonomy should always be hooked to init. If the code in the linked post which I marked duplicate is not working, then you have a serious issue with a plugin or with your theme itself. I personally run that function from a plugin which is where that code should be. Check this post on how to debug the issue. This answer in this linked question should apply here as well – Pieter Goosen Apr 1 '15 at 4:15
  • @PieterGoosen I think you misunderstood my comment. There is a big difference between my problem and that question. In that question it's registering 2 terms on plugin activation, but I need to register terms whenever I need. – Eray Apr 1 '15 at 15:53

You need to insert your term after the init hook (i.e. when the taxonomy is registered):

function create_tax()
    $args = array(...);
    register_taxonomy('custom_tax', array('post'), $args);

    // Now we're safe
    $result = wp_insert_term('Test Term', 'custom_tax', array('parent'=>0) ); 
| improve this answer | |
  • Just edited first code sample and wrapper wp_insert_term with add_new_term() function. Can you check please? I need to put it inside of a function because I have a button on my plugin page , when user click to this button I will add a new term to taxonomy . I'll use AJAX request for this and I'll call add_new_term() function inside AJAX method. – Eray Apr 1 '15 at 16:22
  • 1
    You're still calling add_new_term too early - it has to run after init. Implement your AJAX handler with the API and it'll work. – TheDeadMedic Apr 1 '15 at 16:26
  • If I don't want to use AJAX , are there any action to achieve this? If there isn't I'll use ajax. – Eray Apr 1 '15 at 16:52
  • Of course. Any hook later than init. Give me an example of how/when you need to add the term and I'll suggest a hook. – TheDeadMedic Apr 1 '15 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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