Will McLean
Process diary

Stop users adding terms to a taxonomy

Last modified: October 31, 2017
Created: October 17, 2017

I used this to stop anyone adding terms to a taxonomy that we needed to be set to a set of specific terms. Once they were added to the databases across all environments the below was added to the functions file.


add_action( 'pre_insert_term', 'prevent_terms', 1, 2 );
function prevent_terms ( $term, $taxonomy ) {
    if ( 'term_name' === $taxonomy ) { // You could add more conditionals here based on the user permissions
        return new WP_Error( 'term_addition_blocked', __( 'You cannot add terms to this taxonomy' ) );
    }
 
    return $term;
}

Make sure you update the term_name.

I have lost the source for this, sorry.

This is a Knowledge Base post.