How to not allow users to create new tags, but allow to them to use existing ones

I want a certain role to be able to add new tags, but other users should just be able to use the text bar to search for existing tags.

I know I can hide the tag bar in the sidebar, but I don’t want that. I just want it to say “Sorry, this tag does not exist” or something when someone enters a tag that doesn’t exist.

Is there a plugin or a function that will do that for me?

Solutions Collecting From Web of "How to not allow users to create new tags, but allow to them to use existing ones"

You can hook onto pre_insert_term, check the taxonomy and whether or not the user has the specified role as follows:

function disallow_insert_term($term, $taxonomy) {

    $user = wp_get_current_user();

    if ( $taxonomy === 'post_tag' && in_array('somerole', $user->roles) ) {

        return new WP_Error(
            'disallow_insert_term', 
            __('Your role does not have permission to add terms to this taxonomy')
        );

    }

}

add_action( 'pre_insert_term', 'disallow_insert_term', 10, 2);

This will prevent the user inserting new terms but allow them to search and add existing terms to a post.

Beaware that when on the post edit screen a user can enter in a term name that does not exist and press enter or click the add button which will add the term to the DOM however at this point the term is not added to the database until the user publishes or updates the post at which point the term will be disallowed.