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.

UPDATE – FINAL WORKING CODE

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';
    }
}

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.

Example.

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

TEST:

//CODE
    var_dump($post->post_content);
    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))));

 //RESULTS

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

' (length=59)

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

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

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



//CODE
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)));

 //RESULTS

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *