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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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')
        );

    }

    return $term;

}

add_filter('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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments