Add Word Count to WordPress Admin Columns

Works with Gutenberg.

// Add WordPress WordCount Column
add_filter('manage_posts_columns', 'crunchify_add_wordcount_column');
function crunchify_add_wordcount_column($crunchify_columns) {
    $crunchify_columns['crunchify_wordcount'] = 'Word Count';
    return $crunchify_columns;
}
 
// Show WordCount in Admin Panel
add_action('manage_posts_custom_column',  'crunchify_show_wordcount');
function crunchify_show_wordcount($name) 
{
    global $post;
    switch ($name) 
	{
        case 'crunchify_wordcount':
            $crunchify_wordcount = crunchify_post_wordcount($post->ID);
            echo $crunchify_wordcount;
    }
}
 
// Get individual post word count
function crunchify_post_wordcount($post_id) {
    $crunchify_post_content = get_post_field( 'post_content', $post_id );
    $crunchify_final_wordcount = str_word_count( strip_tags( strip_shortcodes($crunchify_post_content) ) );
    return $crunchify_final_wordcount;
}

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.