我想限制用户只输入两个而不是更多的post\\u tag单词。如果任何标签超过两个单词,用户将不会发布帖子。
我需要用这个函数数数单词。
function count_words( $text ) {
$text2 = preg_replace( \'/<.[^<>]*?>/\', \' \', $text );
$text2 = preg_replace( \'/ | /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;
}
此插件
https://wordpress.org/plugins/wypiekacz/ 有几个规则,但没有标记字数。我该怎么做?
最合适的回答,由SO网友:TheDeadMedic 整理而成
使用pre_insert_term
过滤器:
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 );