PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] How to block certain tags from creating in WordPress

 January 18, 2022     stop-words, tags, taxonomy, wordpress     No comments   

Issue

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

Solution

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


Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing