您可以使用函数add_settings_error
.
更多详细信息可以在Wordpress中找到documentation. 我编辑了您之前的回答,包括:
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 );
您还可以查看Wordpress通知的精美指南
here.