Home > Software engineering >  How to block certain tags from creating in WordPress
How to block certain tags from creating in WordPress

Time:10-31

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

CodePudding user response:

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 );
  • Related