5

I have just get an answer to one of my question but the problem is I want to print an error when every user will insert tags of more than two words through post-new.php with admin_notice action hook.

How can it be done?

1 Answer 1

1

You can use function add_settings_error.

More details can be found in the Wordpress documentation. I have edited your previous answer to include that:

function wpse_189722_limit_tag_words( $term, $taxonomy ) {
    if ($taxonomy === 'post_tag') {
        if ( count( preg_split( '/\s+/', trim( $term ) ) ) > 2 ) {
            add_settings_error('term_too_many_words', 'term_too_many_words', 'Maximum of 2 words allowed, but entered: '. trim($term), 'error');
            // shorten the term to the allowed number of tags
            $normalized_term = $foo = implode(' ', array_slice(preg_split('/\s+/', trim($term)), 0, 2));
            return $normalized_term;
        }
    }

    return $term;
}

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

You can also check a nice guide to Wordpress notifications here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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