WordPress Snippets at WPcustoms

Show word count in dashboard

“This blog contains a total of 722 words!” Show a word count in your admin dashboard or anywhere else on your site with this cool WordPress snippet. Keep in mind that this snippet runs through all your blog posts and may eat up a lot of server ressources.


/**
 * Snippet Name: Show word count in dashboard
 * Snippet URL: https://wpcustoms.net/snippets/show-word-count-dashboard/
 */
  function wpc_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 );
    // This line will add your word count to the bottom of the Right Now box.
    echo '

This blog contains a total of ' . $num . ' words!

'; } // add to Content Stats table add_action( 'right_now_content_table_end', 'wpc_post_word_count'); // add to bottom of Activity Box add_action('activity_box_end', 'wpc_post_word_count');