In this tutorial, you’ll learn how to work with shortcode in WordPress without a plugin. Not sure what shortcode is or how it’s beneficial? Don’t worry, I’ll explain that too.
What is Shortcode in WordPress?
Shortcode gives you the ability to call a custom function directly from a WordPress blog post or page.
Why would you want to do such a thing? A major reason bloggers use shortcode is to access dynamic content.
Specifically in this example, you’ll learn how to add shortcode to count the number of blog posts on your website. Because the number of blog posts on your website changes overtime, you’d have to manually update this number every time you add or remove a blog post.
Instead, we can use shortcode to dynamically count the number of blog posts each time a page is loaded.
The syntax for shortcode is simply the function name surrounded by square brackets like [function]. You may have already seen this syntax before from a plugin that you installed.
How To Add Custom Shortcode to WordPress
The following code snippet defines shortcode that counts the number of blog posts on your WordPress website. The last line of code actually adds that function as shortcode named total_posts. Consequently, we can call this shortcode from within a blog posts like
[total_posts].
// shortcode to count number of posts function wpb_total_posts() { $total = wp_count_posts()->publish; return $total; } add_shortcode('total_posts','wpb_total_posts');
It is recommended that you create your shortcode within a child theme. If you are using GeneratePress, I have a tutorial that walks you through the very easy process of installing a child theme.
Another option is to use a plugin like Code Snippets which allows you to run PHP code on your website. You never want to edit your theme directly as this comes with a risk of breaking things. It’s much safer to use a plugin or work within your child theme instead.
How To Use Shortcode
Finally, the only thing left to do is use the shortcode. To use your custom shortcode, type [total_posts] anywhere in the body of a blog posts or page.
After you publish or preview your post, you’ll notice that the actual number of blog posts is displayed on the page.
Here is my shortcode example in action!
To date, I have published 141 to my website!
Now, each and every time this page is requested, WordPress executes the custom function that we defined. The function returns the total number of blog posts to the page. Then the page inserts this value into the HTML of your blog posts and sends it to the user. Pretty cool, right!
While this is just one simple example of WordPress shortcode, I’m sure you can only imagine the infinite possibilities.

If you have any questions about shortcode in WordPress, let me know in the comments below. I know a lot of times, bloggers are a bit timid to add code to their website. Don’t worry, if you follow the steps above, I assure you that you won’t break anything.
If you found this blog posts valuable, subscribe to my YouTube channel for all my latest WordPress video tutorials and walkthroughs. My goal is to help website owners like you avoid having to pay thousands of dollars to hire a web developer though my free content.