I need to get a total count of characters for post titles and post text characters. Are there any build-in WordPress functions available for that? My posts are paginated so the numbers for post text count it would have to come from DB?

Using these numbers I would like to build a function that will feed my Google Tag Manager dataLayer with things like:

if($postCaharacters < 1000) {
    echo "GTM_POST_CHARACTERS:'Under 1000'";

And so on.


function countWords() {
    global $post;
    $content = $post->post_content;
    $decode_content = html_entity_decode($content);
    $strip_shortcode = strip_shortcodes($decode_content);
    $strip_wp_tags = wp_strip_all_tags($strip_shortcode, true);
    $countwords = str_word_count($strip_wp_tags);
    $number = $countwords;
    if ($number > 3000 || $number == 3000) {
        return '3k+';
    } elseif ($number > 2500 || $number == 2500) {
        return '2.5k-3k';
    } elseif ($number > 2000 || $number == 2000) {
        return '2k-2.5k';
    } elseif ($number > 1500 || $number == 1500) {
        return '1.5k-2k';
    } elseif ($number > 1100 || $number == 1100) {
        return '1.1k-1.5k';
    } elseif ($number > 800 || $number == 800) {
        return '800-1.1k';
    } elseif ($number > 500 || $number == 500) {
        return '500-800';
    } elseif ($number > 200 || $number == 200) {
        return '200-500';
    } else {
        return '<200';
  • I added an example of each function I used, maybe that helps you.
    – Tomás Cot
    Sep 17, 2014 at 22:50

2 Answers 2


You can use this function wp_strip_all_tags.

strlen( wp_strip_all_tags($post->post_content));

With that you get rid of all the HTML tags.

Just a little extra detail, you might want to strip the shortcodes, too. You can use this function strip_shortcodes

You will end up with something like this:

 strlen( wp_strip_all_tags(strip_shortcodes($post->post_content)));

To convert the encoded entities to the corresponding string value you could use the PHP function html_entity_decode.


" " &lt;/S&lt;&gt;;
is converted to
" " </S<>;


    var_dump(wp_strip_all_tags($post->post_content, true));
    var_dump(html_entity_decode(wp_strip_all_tags($post->post_content, true)));
    var_dump(html_entity_decode(strip_shortcodes(wp_strip_all_tags($post->post_content, true))));


string 'áé´r´díó' ñ   " " &lt;/S&lt;&gt;;   a

[gallery]' (length=59)

string 'áé´r´díó' ñ   " " &lt;/S&lt;&gt;;   a [gallery]' (length=56)

string 'áé´r´díó' ñ   " " </S<>;   a [gallery]' (length=47)

string 'áé´r´díó' ñ   " " </S<>;   a ' (length=38)

var_dump(strlen(html_entity_decode(strip_shortcodes(wp_strip_all_tags($post->post_content, true)))));

var_dump(strlen(html_entity_decode(wp_strip_all_tags($post->post_content, true))));

var_dump(strlen(wp_strip_all_tags($post->post_content, true)));


int 38
int 47
int 56

As I was using var_dump each string has it's lenght, but I added the results of the call to the strlen function anyway.

  • Thanks! That's actually working very well! I turned the whole thing into function but for some reason it always returns 0. I tested it with out WP functions functions and it works. See update. Sep 18, 2014 at 20:04
  • @user3088202, where are you using that function? try to add this line var_dump($post);
    – Tomás Cot
    Sep 18, 2014 at 20:48
  • Its in my header.php file at the very top. Should I put it in the functions folder and call it in the page later? Sep 18, 2014 at 21:13
  • you could add the function to the functions.php file, or not. But in the first line of the function add this global $post;, or pass the content of the post as a parameter to the function.
    – Tomás Cot
    Sep 18, 2014 at 21:29

You are right in assuming that $post->post_content contains tags and other not textual entities like shortcodes. You can use strip_tags to get rid of the tags but you will probably want to expand the shortcodes first. resulting in something like

$count = strlen(strip_tags(do_shortcode($post->post_content));

And it will still give you the wrong results depending on whether spaces quotese etc need to be counted.

  • I see what you saying about quotes and etc. Right now -> Don't == Don&#8217;t so it counts extra chars. I will have to str_replace before everything else. Is there a good list of characters that I can just pop into that function? Sep 17, 2014 at 17:06
  • I am actually not sure that the "text" in the DB is converted to HTML therefor you don't need to worry about converting &#8217 to ', but if I am wrong you can use html_entity_decode which is a php function. Sep 17, 2014 at 18:32
  • I tried using that function on the get_the_title() but it still returns &#8217. Sep 17, 2014 at 19:12
  • @user3088202, maybe this is what you are looking for html_entity_decode
    – Tomás Cot
    Sep 17, 2014 at 20:37
  • That is the one I used but it still didn't get rid of anything. Sep 17, 2014 at 21:05

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.