Getting term_id for newly created or edited term

I’m taking my first foray into plugin development and have got confused pretty quickly. I’m attempting to make a simple plugin that stores hex colours against terms, so you can assign a colour to a tag or category and then use that in a theme.

What I cant figure out is how to get the term_id of a newly created or edited term. I’ve bolted on a form to ‘edit_tag_form’ with a colour picker and can access these values through $_POST, but if it’s a newly created tag I don’t see how my code can know the ID of the newly created one. I need to know the term_id to be able to link it with the hex colour and save it to wp_options.

Read More

I’m using the action hook ‘edited_term’. I guess for edited existing terms I could get the tag_ID from the query string, but I want to be able to assign colours immediately to newly created tags/categories.

Related posts

Leave a Reply

1 comment

  1. Use the action created_term. Its first parameter is the $term_id.

    It is called in wp_insert_term() in wp-includes/taxonomy.php after a term was successful created:

    do_action("created_term", $term_id, $tt_id, $taxonomy);
    do_action("created_$taxonomy", $term_id, $tt_id);
    

    The second parameter is the term_taxonomy_id from the term_taxonomy table, and the last parameter is the taxonomy.

    So register an action with …

    add_action( 'created_term', 'wpse_78858_add_color', 10, 3 );
    

    … and your callback should look like this:

    function wpse_78858_add_color( $term_id, $tt_id, $taxonomy )
    {
        # do something
    }