WordPress is a very good content management system. I once built a WordPress-based news site for a company with multiple editors. The editor-in-chief had word count requirements for every article, but their old CMS could not display article length, so editors had to count words manually in Word. Once I understood that pain point, it was clear that adding a word count column to the post list would be simple and useful.

Count the number of words in a post
First, we need to calculate the number of words in the post. The function below takes the post content and returns the word count. It can also be used on the front end if you ever need to display the word count in the theme.
function count_words ($text) {
global $post;
if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8') $output .= mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
return $output;
}
Save the counted word total as a custom field
This step retrieves the post content and stores the word count. One imperfect part of this approach is that the post must be saved once, and the count is only available after that save occurs.
add_action('save_post', 'words_count', 0);
function words_count($post_ID) {
$content_post = get_post($post_id);
$content = $content_post->post_content;
$words_count = count_words($content);
update_post_meta($post_ID, 'words_count', $words_count);
}
Display it in the post list
This step retrieves the saved word count field from step two and displays it in the post list.
add_filter('manage_posts_columns', 'posts_column_words_count_views');
add_action('manage_posts_custom_column', 'posts_words_count_views',5,2);
function posts_column_words_count_views($defaults){
$defaults['words_count'] = __('字数', 'wizhi');
return $defaults;
}
function posts_words_count_views($column_name, $id){
if($column_name === 'words_count'){
echo get_post_meta(get_the_ID(), 'words_count', true);
}
}
Besides word count, we can use the same pattern to add other kinds of data to the post list, such as post thumbnails, post view counts, or custom action links and buttons for faster workflows.
