WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

By submitting a post for publication will be made a count of the number of words in the post and will add a div with font-size pre-set according to the number of words.

If the post contains up to 50 words, returns with font-size:18px;

<div style="font-size:18px;"><p>Text with up to 50 words.</p></div> 

If the post contains 50 to 100 words, returns with font-size:16px;

<div style="font-size:16px;"><p>Text with 50 to 100 words.</p></div>

The span will be closed after the last </p>.

<div style="font-size:16px;"><p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p></div>

Does anyone know of any function to it? Thank you.

share|improve this question
up vote 3 down vote accepted

Filter 'the_content', count the words, and add the markup you need. You should not use str_word_count() because it has issues with numbers and utf-8.

So let’s start with a word count function:

 * Alternative for str_word_count().
 * @link http://toscho.de/2012/php-woerter-zaehlen/
 * @param string $str
 * @return int Number of words
function t5_word_count( $str )
    // We do not want to count markup
    $text = strip_tags( $str );
    // no leading and trailing white space
    $text = trim( $text );
    // take multiple white spaces as one
    // Add one space to get an almost exact match
    $word_count = preg_match_all( '~\s+~', "$text ", $m );

    return (int) $word_count;

Now the filter function:

// Hook in very late to let other filters (shortcodes and such)
// do their work first.
add_filter( 'the_content', 't5_content_word_count', 999 );

 * Add a <div> with a special class to the content.
 * @param string $content
 * @return string
function t5_content_word_count( $content )
    $words = t5_word_count( $content );
    $class = 'default';
    // max 100 words
    $words < 101 && $class = '100';
    // max 50 words
    $words < 51  && $class = '50';
    return "<div class='word-count-$class'>$content</div>";

Instead of inline styles which are very bad on mobile devices or printed pages I have chosen a class.

In your stylesheet you can now style these classes:

    font-size: 1em;
    font-size: 1.1em;
    font-size: 1.2em;

Be aware that the filter works on single pages of a post only. If you have split a post into multiple parts with <!--nextpage--> you may get different classes for different pages of that post.


To insert the div when a post is saved do not filter 'the_content', but 'wp_insert_post_data'. And you need a third function for this:

add_filter( 'wp_insert_post_data', 't5_count_words_on_insert', 999 );

 * Add the font size div on post insert.
 * @wp-hook wp_insert_post_data
 * @param   array $data
 * @return  array
function t5_count_words_on_insert( $data )
    '' !== $data['post_content']
        && FALSE === strpos(
                stripslashes( $data['post_content'] ),
                "<div class='word-count-"
        && $data['post_content'] = t5_content_word_count( $data['post_content'] );

    return $data;

See /wp-includes/post.php function wp_insert_post() for details.

I do not recommend this solution. You cannot use <!--nextpage--> now anymore because you would end up with an unclosed <div> on the first page.

share|improve this answer
Thank you @toscho Was researching wp_insert_post_data. Now I add a conditional: if the post is of the category x do nothing. – BóbGCA Jun 9 '12 at 23:01

the php function for this is


Where string = (required) and specifies the string to check

return = (Optional). Specifies the return value of the str_word_count() function. Possible values: 0 - Default. Returns the number of words found, 1 - Returns an array with the words from the string, 2 - Returns an array where the key is the position of the word in the string, and value is the actual word

char = (Optional) Specifies special characters to be considered as words.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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