Skip to content

Instantly share code, notes, and snippets.

@antonlukin
Last active March 26, 2019 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonlukin/dd22530f180300e67385025568521081 to your computer and use it in GitHub Desktop.
Save antonlukin/dd22530f180300e67385025568521081 to your computer and use it in GitHub Desktop.
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