Site logo

Archived topic

Is There A Way to Create a "Word Count" Column in the Admin All Post Panel?

7 replies · Started by Ken on December 28, 2021

Viewing posts 1–8 of 8

Latest GeneratePress with Child Theme

I see there are plugins that will add a 'word-count' column to the All Posts Panel.

Is there some magic php or coding that I could use that will add a post word-count column to the panel and be able to sort it (Sorting not necessary if this makes it too complicated

I looked on the internet and the answers I found gave me a parsing error.
Here's the code I used that didn't work:

?php

add_action('save_post', function($post_id, $post, $update) {
	$word_count = explode(" ", strip_shortcodes($post->post_content));

	update_post_meta($post_id, '_wordcount', count($word_count));
}, 10, 3);

add_filter('manage_posts_columns', function($columns){
	$columns['wordcount'] = 'Word count';

	return $columns;
});

add_action('manage_posts_custom_column', function($name) {
	global $post;

	$wordcount = get_post_meta($post->ID, '_wordcount', true);
  
	if($wordcount === '') {
  		$wordcount = 'not counted';
	}

	if($name === 'wordcount') {
		echo $wordcount;
	}
});

Hi Ken,

Is this the exact code? If so, The code you've shared seems to have a syntax error as the first line ?php is missing the < part.

It should be <?php.

Hi Elvin,
Yes the original code had " < ".
When I pasted to my GP Child Functions.php, that included the " < ", it didn't work, I got a parsing error.
Here's the correct code I pasted in (" < " included this time)

<?php

add_action('save_post', function($post_id, $post, $update) {
	$word_count = explode(" ", strip_shortcodes($post->post_content));

	update_post_meta($post_id, '_wordcount', count($word_count));
}, 10, 3);

add_filter('manage_posts_columns', function($columns){
	$columns['wordcount'] = 'Word count';

	return $columns;
});

add_action('manage_posts_custom_column', function($name) {
	global $post;

	$wordcount = get_post_meta($post->ID, '_wordcount', true);
  
	if($wordcount === '') {
  		$wordcount = 'not counted';
	}

	if($name === 'wordcount') {
		echo $wordcount;
	}
});

And here's the error I get using the above code;

Parse error: syntax error, unexpected '<', expecting end of file in /home/u577764507/domains/homecomfortandjoy.com/public_html/wp-content/themes/generatepress_child/functions.php on line 9
There has been a critical error on this website.

Learn more about troubleshooting WordPress.

Did that fix it? Great idea btw

That issue on line 9 is probably the <?php.

Since it's on a child theme's functions.php, there's likely a <?php so there's no need for another one of that unless the first one was delimited.

Can you try removing the <?php in this code specifically?

Elvin and Jeff, found some code that works! ٩(^ᴗ^)۶
First, Elvin, sorry to trouble you. I just had to click a few more links in G-Search and I'd have my answer. But as always, I appreciate your help.
Here's the link to the page that has the code.

There are some instructions to help tweek where one would like the word-count column to show up. I chose to have the word-column show up after the post title. Simple, easy copy-paste to the Child's Function.php file.

I'm going to mark this post as Resolved ٩( ᐛ )و

Hi Elvin,
Ah, I saw your post after I clicked the 'resolved' button. Sorry.
The link I found and mentioned earlier is what I used successfully and that webpage provides some nice options for where and how to display the word-count column.

Thank you again for your help. All is good. Wising you good folks a healthy, prosperous New Year.

No problem. Thanks for letting us know. :D

This archived topic is closed to new replies.