Unfortunately, this is not possible, at least not safe.
The function wp_insert_term( $term, $taxonomy, $args = array() )
that handles the creation of taxonomies provides a few filters and hooks, but the only one before the creation of the term in the database is
$term = apply_filters( 'pre_insert_term', $term, $taxonomy );
As you can see, the $args
provided in the creation of the term are not passed to the filter – so you have no way of checking how many levels of the taxonomy exists by looking up $args['parent']
.
An ugly way of doing it would be after the term is created, hooking into the action created_term
or create_term
, and checking it there. I have never done this myself, and you may open up a big can of worms deleting the term after creation with no possibility of returning an error afterwards.
If the filter pre_insert_term
would accept the $args
as an argument, it would me no problem.