How to Create a Custom Word Count Shortcode in WordPress

WordPress empowers website owners to enhance their websites with custom functionalities, even without advanced programming skills. In this tutorial, you’ll learn how to create a custom shortcode in WordPress to display the word count of your posts or pages. This practical feature can be a valuable addition to your website, providing your readers with useful information. Best of all, you won’t need to rely on third-party plugins โ€“ you’ll create a custom shortcode.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Access Your Theme’s functions.php File and Define the Shortcode

To get started, you’ll need to access your WordPress theme’s functions.php file. You can do this through a code editor or use the built-in Theme Editor within your WordPress dashboard. Once you’ve opened the functions.php file, add the following well-commented code snippet at the end:

// Define a custom word count shortcode function
function custom_word_count_shortcode() {
    // Get the post content
    $content = get_post_field('post_content', get_the_ID());
    // Count the words in the content, excluding HTML tags
    $word_count = str_word_count(strip_tags($content));
    // Return the word count
    return $word_count;
}

// Register the custom word count shortcode
add_shortcode('word_count', 'custom_word_count_shortcode');
Code language: PHP (php)

Explanation:

  • We start by defining a custom function named custom_word_count_shortcode.
  • Inside the function, we fetch the post content using get_post_field.
  • Word counting is done using str_word_count, and we ensure it excludes HTML tags by utilizing strip_tags.
  • Finally, we return the word count.
  • We register the shortcode using add_shortcode, specifying the shortcode name as ‘word_count’ and the callback function as ‘custom_word_count_shortcode’.

Use the Shortcode in Your Posts

Now that the [word_count] shortcode is set up, you can effortlessly employ it within your posts or pages. Simply insert [word_count] into the content where you desire the word count to appear.

Save Changes and Test

After incorporating the shortcode into your content, remember to save your changes. Proceed to visit a post or page containing the [word_count] shortcode, and you’ll witness the word count elegantly displayed.

That’s it! By creating a custom word count shortcode in WordPress, you’ve empowered your website with a practical feature that enhances user engagement and content accessibility. This customization approach keeps your website efficient and allows you to provide valuable information to your readers without the need for third-party plugins.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

Your email address will not be published. Required fields are marked *