Arshpreet Singh
Arshpreet Singh

Reputation: 15

How to block certain tags from creating in WordPress

Is there any WordPress theme function code that blocks certain wordpress tags from being created? I'd want to exclude some tags from the keyword list, for example, I don't want WordPress to create the following stop-words tags:

adult, bikini, enjoyment, fun, block, admin

Upvotes: 0

Views: 294

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use the pre_insert_term filter hook. that will help you to prevent tags before inserting. try the below code.

function prevent_some_tags_from_being_add( $term, $taxonomy ){
    if( $taxonomy == 'post_tag' ){

        $prevent_tags = array( 'adult', 'bikini', 'enjoyment', 'fun', 'block', 'admin' );

        if( in_array( $term, $prevent_tags ) ){
            return new WP_Error( 'invalid_term', __( 'Sorry this tag is not allowed.' ) );
        }
    }

    return $term;
}

add_filter( 'pre_insert_term', 'prevent_some_tags_from_being_add', 10, 2 );

Upvotes: 3

Related Questions