0

I have a plugin that successfully creates a custom taxonomy for a custom post type.

What I'd like to do, is on activation (or wherever works), create a default set of terms for the custom taxonomy. I've successfully done this before by using wp_insert_category in a function called upon plugin activation.

That same function isn't working for the custom taxonomy, though. My guess is that it is because the custom taxonomy isn't registered yet when the function gets fired.

I can't seem to find out where to hook the function so that it works with a custom taxonomy. I've tried calling the function from wp_loaded also and I curiously get Call to undefined function wp_insert_category() That puzzles be as well because I thought that hook meant all of Wordpress was loaded.

Any advice would be greatly appreciated.

Thanks.

1 Answer 1

2

Hook into registered_taxonomy. This is called after the taxonomy was registered successfully.

Sample code:

add_action( 'registered_taxonomy',  'insert_default_terms', 10, 3 );

function insert_default_terms( $taxonomy, $object_type, $args )
{
    if ( 'your_tax_name' !== $taxonomy )
        return;

    // insert terms
}
2
  • Thank you. I'm using that hook and have added the code to insert a custom category with // insert terms $my_cat = array( 'cat_name' => 'test category', 'taxonomy' => 'custom_taxonomy' ); // Create the category wp_insert_category($my_cat); But I get the error Fatal error: Call to undefined function wp_insert_category() again, which is very odd.
    – edeneye
    Nov 6, 2013 at 22:42
  • I read that other people had had issues calling wp_insert_category() from a function, so I changed to wp_insert_term() and things seem to be working now. Thanks.
    – edeneye
    Nov 6, 2013 at 22:48

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.