Created
February 15, 2019 11:01
-
-
Save imelgrat/8a334eb3d9a45d2e3413681e635f1d08 to your computer and use it in GitHub Desktop.
Add a word-count column to WordPress Posts List (Dashboard view).
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 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