How to Add Post Word Count Stats to WordPress Without a Plugin

How to Add Post Word Count Stats to WordPress

When writing content for your WordPress website, it’s important to have a minimum word count for each article. Generally, it is recommended to have a minimum of 1,000 words per article but you should be pushing more to the 2,000+ words mark. However, this can vary and depends very much on your article objectives. A study completed by Noah Kaga in 2018, looked at 100 million articles and found that the longer the content, the more shares it gets. Analysis completed by backlinko in 2016, of one million Google search engine rankings found that long-form content ranks significantly better than short-form articles and the average word count on Google’s front page is 1,890 words.

Therefore, before publishing new content to your WordPress website it’s a good idea to check the number of words. If you write content directly into the WordPress editor, you can check the number of words directly in the editor. If you are using the Gutenberg editor, click on the information icon to get the number of words in your article and if you are using the Classic editor, then the word count appears in the bottom status bar as shown below.

Gutenbery Editor

 
Word Count Gutenberg Editor

Classic Editor

 
Word Count Classic Editor

Although WordPress provides the functionality to check the number of words per post, if you have already published content yourself or run a multi-author WordPress site with previously published content then it would consume a significant amount of time to check every individual post word count.

In this guide, we will show you how to add an extra column to the WordPress post admin screen so you can easily see the total word count for all your posts; published, draft or trash, by creating our own WordPress function using functions.php.

IMPORTANT In this tutorial, we'll be directly editing WordPress theme files and we recommend that you create a child theme of your existing parent theme. By using a child theme you will be able to apply modifications without altering the parent theme files and ensuring any changes you make are kept following any parent theme updates.
TIP If you don't feel comfortable with editing the functions.php file directly, we would recommend you use the Code Snippets plugin. This plugin will enable you to easily add, manage and delete WordPress code snippets from your dashboard. To find out more about adding custom code snippets using a plugin see our tutorial How to Add Custom Code Snippets to WordPress with the Code Snippets Plugin for more information.

Post Word Count Stats

We are going to add Word Count stats directly into your WordPress post admin screen using custom code based on some WordPress functions. To add the code we will editing the functions.php file. You can edit the functions.php file directly in the WordPress dashboard under Appearance > Theme Editor > Theme Functions or edit the file offline and upload using your favourite FTP program.

Once you have entered the code options below into your functions.php file. Log into your WordPress dashboard and go to your post admin screen to see the results below.

Admin Word Count After

Add Post Word Count Stats (Default)

If you want to add post word count stats using the default column location (after Date), you can simply copy and paste the following code into your functions.php file:

// Count the words
function vpsb_post_word_count($post_id)
{
    $content = get_post_field('post_content', $post_id);
    $word_count = str_word_count(strip_tags(strip_shortcodes($content)));
    return $word_count;
}
add_filter('manage_posts_columns', 'vpsb_wordcount_column');

// Add custom column
function vpsb_wordcount_column($columns)
{
    $columns['wordcount'] = 'Word Count';
    return $columns;
}
add_action('manage_posts_custom_column', 'vpsb_show_wordcount');

function vpsb_show_wordcount($name)
{
    global $post;
    switch ($name) {
        case 'wordcount':
            $wordcount = vpsb_post_word_count($post->ID);
            echo $wordcount;
    }
}
TIP If you wanted to change the column title you can edit the $columns['wordcount'] = 'Word Count'; line to anything you want, for example, if you wanted it to display Total Words you would change the code to $columns['wordcount'] = ‘Total Words’;.

Add Post Word Count Stats (Custom)

If you want to add post word count stats using the custom column location (after Title, although you can change to your desired location), you can simply copy and paste the following code into your functions.php file:

// Count the words
function vpsb_post_word_count($post_id)
{
    $content = get_post_field('post_content', $post_id);
    $word_count = str_word_count(strip_tags(strip_shortcodes($content)));
    return $word_count;
}
add_filter('manage_posts_columns', 'vpsb_wordcount_column');

// Add custom column
function vpsb_wordcount_column($columns)
{
    $columns['wordcount'] = 'Word Count';
    return $columns;
}
add_action('manage_posts_custom_column', 'vpsb_show_wordcount');

function vpsb_show_wordcount($name)
{
    global $post;
    switch ($name) {
        case 'wordcount':
            $wordcount = vpsb_post_word_count($post->ID);
            echo $wordcount;
    }
}

// Reordering columns
function vpsb_add_wordcount_column($defaults)
{
	$new_columns = array(); // Create empty array to store new column order

	foreach ($defaults as $key => $value)
	{
		$new_columns[$key] = $value; // Add columns to new array
		if ($key == 'date')
		{ 
		    // when we find the title column
			$new_columns['wordcount'] = 'Word Count'; // Add the word-count column after title column
		}                
	}

	return $new_columns;
}

add_filter('manage_posts_columns', 'vpsb_add_wordcount_column');
TIP If you wanted to change the column location in the WordPress post admin screen you can edit the if ($key == 'date') line, for example, if you want to display the column after the Title you would change the code to if ($key == ‘title'). Similarly, if you wanted to change the column title you can edit the $new_columns['wordcount'] = 'Word Count'; line to anything you want, for example, if you wanted it to display Total Words you would change the code to $new_columns['wordcount'] = ‘Total Words’;.

That’s it. You have now successfully added an extra column to the post admin screen to display the post word count and changed the column title and location. You have also added a minimum word count to all new posts published and changed the threshold level.

VPSBasics

VPSBasics

This guide was written by the VPS Basics editorial team, led by Gilberto Van Roosen. They are a unique blend of people who are dedicated to providing highly detailed, comprehensive and easy to follow tutorials, written in plain English. They specialise in tutorials for managing Linux servers, its software and WordPress.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.