Skip to content

Instantly share code, notes, and snippets.

@imelgrat
Created February 15, 2019 10:56
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/a3b162f12c5748578178672b3a996fd6 to your computer and use it in GitHub Desktop.
Save imelgrat/a3b162f12c5748578178672b3a996fd6 to your computer and use it in GitHub Desktop.
Custom function that takes a WordPress post ID and returns the number of words in the post.
<?php
/**
* Count Post Words.
*
* Custom function that takes a WordPress post ID and returns the number of words in the post.
* You should paste it on your theme's functions.php file.
*
* @link https://imelgrat.me/wordpress/customize-wordpress-post-management-page/
*
* @param int $post_id The Post ID
*
* @return int The word count.
*/
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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment