How to Get Word Count Stats on Your WordPress Post Section

Manuel Campos

Word count could be an important metric to track when managing a WordPress website, especially if you publish a lot of content regularly.

By default, WordPress doesn’t display word count information in the dashboard, but with a little bit of code, you can add this feature and easily monitor the length of your posts.

Follow the steps below to enable word count stats on your WordPress dashboard.


How to Get Word Count Stats on Your WordPress Dashboard

Step 1: Access your WordPress dashboard, navigate to Appearance > Theme Editor, and open the functions.php file.

Step 2: Add the code Copy and paste the following code at the end of the functions.php file:

add_filter('manage_posts_columns', 'wps_add_column');
function wps_add_column($wps_wordcount_column) {
    $wps_wordcount_column['wps_wordcount'] = 'Word Count';
    return $wps_wordcount_column;
}

add_action('manage_posts_custom_column', 'wps_display_wordcount');
function wps_display_wordcount($name) {
    global $post;
    switch ($name) {
        case 'wps_wordcount':
            // Get the post ID and pass it into the get_wordcount function
            $wps_wordcount = wps_get_wordcount($post->ID);
            echo $wps_wordcount;
    }
}

function wps_get_wordcount($post_id) {
    // Get the post, remove any unnecessary tags and then perform the word count
    $wps_wordcount = str_word_count(strip_tags(strip_shortcodes(get_post_field('post_content', $post_id))));
    return $wps_wordcount;
}

Step 3: Save the changes After adding the code, click the “Update File” button to save the changes to your functions.php file.

Step 4: Check the word count stats Now, navigate to the “Posts” section in your WordPress dashboard. You should see a new column labeled “Word Count” displaying the word count for each post.

The word count is calculated by excluding any unnecessary tags and shortcodes from the post’s content.

Additional Considerations

These are some things you should take into account when adding code snippets to the functions.php

  • It’s important to exercise caution when modifying the core functionalities of WordPress. Always make a backup of your files before making any changes.
  • If you are using a child theme, it’s recommended to place the code snippet in the child theme’s “functions.php” file to ensure compatibility and avoid losing the changes when updating the parent theme.
Manuel Campos, English Professor

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica