Do you want to show the total count of published words to the bottom of the “Right Now” widget in WordPress admin dashboard? While there’s probably a plugin for this, we have created a quick code snippet that you can use to display post word count in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function post_word_count() { $count = 0; $posts = get_posts( array( 'numberposts' => -1, 'post_type' => array( 'post', 'page' ) )); foreach( $posts as $post ) { $count += str_word_count( strip_tags( get_post_field( 'post_content', $post->ID ))); } $num = number_format_i18n( $count ); // This block will add your word count to the stats portion of the Right Now box $text = _n( 'Word', 'Words', $num ); echo "<tr><td class='first b'>{$num}</td><td class='t'>{$text}</td></tr>"; // This line will add your word count to the bottom of the Right Now box. echo '<p>This blog contains a total of <strong>' . $num . '</strong> published words!</p>'; } // add to Content Stats table add_action( 'right_now_content_table_end', 'post_word_count'); // add to bottom of Activity Box add_action('activity_box_end', 'post_word_count');
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 7 best WordPress poll plugins to grow onsite engagement and 12 best WordPress podcast plugins.
Is it possible to add this to the actual blog with a template tag?
Hi Justin yes you can do that, should be able to just add to your template file and this will be displayed. Keep in mind this script adds HTML you will want to change for this to work in your theme nicely.
[…] This post was mentioned on Twitter by Michael Davis, WPSNIPP. WPSNIPP said: #wordpress Post word count http://bit.ly/gULp1N #blog please RT 🙂 […]