Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Posts word count in WordPress admin
<?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;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment