0

I have a function working on 'create_term' hook, I'm trying to prevent
the term from being created on certain situations, I've tried

return false;

But that didn't do much, how do I prevent the term from being created?

3

create_term as you can see in inline docs in code

Fires immediately after a new term is created, before the term cache is cleaned

So you can't prevent an event after that event already happen.

To prevent a term is inserted you can use 'pre_insert_term' filter hook and return a WP_Error a 0 or an empty string.

Example:

add_filter( 'pre_insert_term', 'prevent_add_term', 20, 2 );

function prevent_add_term( $term, $taxonomy ) {
  if ( $term === 'i-am-a-bad-term' ) {
    $term = new WP_Error( 'invalid_term', 'Term you tried to add sucks' );
  }
  return $term;
}
2
  • good, concise, and I wish the docs would've covered hooks a bit better.
    – Asaf
    Jun 13 '14 at 19:53
  • @Asaf all of us hope that ;)
    – gmazzap
    Jun 13 '14 at 21:16
1

create_term is only fired after the term has already been inserted, so returning false will do nothing.

pre_insert_term appears to be the hook to use, as mentioned in the codex here. However, the specific hook seems undocumented, so you might have to play with it a bit.

1
  • 1
    Aaand see G.M.'s simultaneous answer, more thorough than mine.
    – vancoder
    Jun 13 '14 at 19:38

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.