CMS Mastering Development Wordpress

Allow user only create specific tags

I am using an editorial wordpress theme that has a frontend post editor for a user (contributor) to create a post. The editor is giving permission to the user to choose category for the article and to add tags.

I have set specific custom tags that could be assigned with the post.

How can i allow the user to only be able to add those tags and NOT any tag that he wants. For examlpe only ‘exodus’ ‘holidays’ . Dont need to create any msg for the user. Just allow to register in the database only those specific tags. Could i use the ID of the custom tags that i created somehow?

I found something very similur in this post and it works fine from the backend post editor, but from the front it doesn’t.

function disallow_insert_term($term, $taxonomy) {

$user = wp_get_current_user();

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

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


return $term;
add_filter('pre_insert_term', 'disallow_insert_term', 10, 2);

*(Note that the contributor can not publish, only send for approval an article. But when i publish the article i don’t get any tags.)

Or maybe something like this but instead of removing specific tags,remove all tags except the custom ones.

function remove_tags_function( $post_id ){
    $post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
    $pos = array_intersect( array('TAG1', 'TAG2', 'TAG3', 'ETC...'), $post_tags ); //check for the prohibited tag
    if( !empty($pos) ) { //if found
        $post_tags = array_diff($post_tags, $pos);
        wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset

add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function

Also the above code works fine by removing the ‘TAG1’, ‘TAG2’, ‘TAG3’, ‘ETC…’ tags, but still the tag is being created in post_tag taxonomy.

So basically i am searching for a filter that allows only specific tags to be added to the database when a user starts typing tags.

Any help would be appreciated

Leave a Reply

Your email address will not be published. Required fields are marked *