1

I want to limit user to enter post_tag only words of two not more that. If any of the tag is more than two words the user caanot publish the post.

I need to use this function to count word.

function count_words( $text ) {
    $text2 = preg_replace( '/<.[^<>]*?>/', ' ', $text );
    $text2 = preg_replace( '/&nbsp;|&#160;/i', ' ', $text2 );
    $text2 = preg_replace( '/[0-9.(),;:!?%#$¿\'"_+=\\/-]*/', '', $text2 );
    $text2 = trim( $text2 );
    if ( $text2 == '' ) {
        $count = 0;
    } else {
        $count = preg_match_all( '/\S\s+/', $text2, $matches );
        if ( $count !== false ) {
            $count += 1;
        } else {
            $count = -1; // Error!
        }
    }
    return $count;
}

this plugin https://wordpress.org/plugins/wypiekacz/ has several rules but not tag word count. How can I do that?

2

Use the pre_insert_term filter:

function wpse_189722_limit_tag_words( $term, $taxonomy ) {
    if ( $taxonomy === 'post_tag' ) {
        if ( count( preg_split( '/\s+/', trim( $term ) ) ) > 2 )
            $term = new WP_Error( 'term_too_many_words', 'Maximum of 2 words' );
    }

    return $term;
}

add_filter( 'pre_insert_term', 'wpse_189722_limit_tag_words', 10, 2 );
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.