Admin & Dashboard PHP Beginner

Add a Word Count Column to Posts

Show how many words each post has in the Posts list, to spot thin content quickly.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 13 lines
add_filter( 'manage_posts_columns', function ( array $columns ): array {
	$columns['nahnu_words'] = 'Words';
	return $columns;
} );

add_action( 'manage_posts_custom_column', function ( string $column, int $post_id ): void {
	if ( 'nahnu_words' !== $column ) {
		return;
	}
	$text  = wp_strip_all_tags( strip_shortcodes( (string) get_post_field( 'post_content', $post_id ) ) );
	$count = count( preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY ) );
	echo esc_html( number_format_i18n( $count ) );
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Short posts often rank poorly and need expanding. A word count column lets you spot them in the list without opening each one.

How it works

The column strips HTML and shortcodes from each post’s content and counts words by splitting on whitespace, which works for most languages that use spaces. The number is formatted for your site’s locale.

Things to watch

  • Content built with page builders may be stored in custom fields rather than post content, so those posts can show low counts.
  • Counting runs for each post on the current page of the list. With 20 posts per page that’s instant.

How to test it

Open Posts → All Posts. A Words column should show each post’s length.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only