Last active
March 26, 2019 20:47
-
-
Save antonlukin/dd22530f180300e67385025568521081 to your computer and use it in GitHub Desktop.
Posts word count in WordPress admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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