How To Show Total Word Count In WordPress Dashboard

WordPress doesn’t show the number of words your site or blog has combined, but thankfully there is an easy solution for this. With a small piece of PHP, we can easily get the total word count of our site or blog.

WordPress total word count function

To see the total word count of our WordPress site, we need to add the following code to our functions.php file. This can be done through your hosting’s file editor or through the theme editor in WordPress.

/* Total word count */
add_action( 'dashboard_glance_items', 'bloginbox_wordcount_at_a_glance' );
function bloginbox_wordcount_at_a_glance() {
    
    $count = 0;
    $text = " words";
    $posts = get_posts( array(
            'numberposts' => -1,
            'post_type' => 'post'
    ));
    
    foreach($posts as $post) {
        $count += str_word_count(strip_tags(get_post_field('post_content', $post->ID)));
    }
    $num = number_format_i18n($count);
    $total = '<li class="word-count"><a href="edit.php">'.$num.$text.'</a></li>';
    echo $total;
}

How to include pages to total word count

If we want to include pages to this we need to change the ‘post_type’ => ‘post’ to ‘post_type’ => ‘any’. By doing this the total word count will include words from pages as well.

Conclusion

In conclusion, adding the total word count to your WordPress dashboard is not hard, but it does require a small amount of code to calculate the total. When added successfully, you can see the total word count of your site or blog in the “At a Glance” widget in your dashboard area.

Similar Posts

Leave a Reply

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