1

I am hijacking create_term to do stuff when a new term is created...

// Fire the function when new term is created
add_action( 'create_term', 'process_term', 11, 3 );

function process_term($term_id, $tt_id, $taxonomy) {

    if ($taxonomy == 'post_tag') {
        // Do stuff
    }

}

By means of experimentation and learning, I'd like // Do stuff to include displaying a feedback confirming that we saw a new 'post_tag' term was created.

I have read that create_term does not support echoing anything back to the browser. Indeed, Doing a simple in the function breaks something in display...

echo $taxonomy;

enter image description here

In my case, IF the term was created in the admin backend (ie. either in the edit-tags.php Tags section or in the post-new.php or similar Post edit screen), I'd like the feedback to take the form of a WordPress alert notice bar on the subsequent screen.

enter image description here

"Subsequent" is an interesting question, I suppose, because the edit-tags.php page doesn't refresh to a new page when a term is created on it. And I guess that is the same for post-new.php, sometimes (?). They often update in-situ.

I'd settle for a simple echo in order to understand whether doing anything out of create_term is possible. But I guess that's what a "notice" really is... you echo out HTML with the notice class, right?

With that in mind, the following code, which places admin_notices inside my create_term function, does not produce broken displayed code - but it also does not display any notice message at all, suggesting admin_notices isn't even firing...

// Fire the function when new term is created
add_action( 'create_term', 'process_term', 11, 3 );

function process_term($term_id, $tt_id, $taxonomy) {

        add_action('admin_notices', 'my_admin_notice');

}



function my_admin_notice() {
    ?>

    <div class="notice notice-success is-dismissible">
        <p>This is a success message.</p>
    </div>

    <?php
}
3
  • I was recently trying to accomplish something similar.
    – Mark
    Aug 24 '19 at 18:22
  • @Mark In my case, turns out I also need to remove an error being thrown in the admin by Ajax when the moved term cannot be found. As well as adding a new notice. So I'm wondering if wp_ajax_add_tag() has a role to play - wordpress.stackexchange.com/questions/345813/… Aug 25 '19 at 6:59
  • Sorry about that. I'd started a comment and got pulled away. Didn't realize it posted! Comment was supposed to say that I ended up adding custom admin columns to CPT and tax indicating whether or not some meta had been correctly added to the term.
    – Mark
    Aug 27 '19 at 3:47

Your Answer

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

Browse other questions tagged or ask your own question.