Skip to content

Instantly share code, notes, and snippets.

@imelgrat
Created February 15, 2019 11:01
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 imelgrat/8a334eb3d9a45d2e3413681e635f1d08 to your computer and use it in GitHub Desktop.
Save imelgrat/8a334eb3d9a45d2e3413681e635f1d08 to your computer and use it in GitHub Desktop.
Add a word-count column to WordPress Posts List (Dashboard view).
<?php
/**
* Add a word-count column to WordPress Posts List (Dashboard view).
*
* Custom code for adding a new column to the posts list.
* The new column will contain the number of words in the post
*
* @link https://imelgrat.me/wordpress/customize-wordpress-post-management-page/
*/
function post_word_count($post_id)
{
$content = get_post_field('post_content', $post_id);
$word_count = str_word_count(strip_tags(strip_shortcodes($content)));
return $word_count;
}
add_filter('manage_posts_columns', 'wordcount_column');
function wordcount_column($columns)
{
$columns['wordcount'] = 'Word count';
return $columns;
}
add_action('manage_posts_custom_column', 'show_wordcount');
function show_wordcount($name)
{
global $post;
switch ($name) {
case 'wordcount':
$wordcount = post_word_count($post->ID);
echo $wordcount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment