How To Create A Shortcode In WordPress

Illustration by Icons 8 from Ouch!

When you’re feeling like adding some extra features to your website, adding a shortcode is a great way to do it. But how do you create a shortcode for your WordPress site? In this article, we will teach you how to create a shortcode in WordPress

Table of Contents

What are shortcodes

Shortcodes are pieces of code that can quickly be added to various areas of your WordPress site, giving you the opportunity to fully customize the functionality available. While shortcodes can be complicated and time-consuming to make, there are many tools available on the internet that make the process much easier. We’ll go over how to do it in this article!

How to create a function

Before we create a shortcode we should create a function. A function is the piece of the code that will be executed when the shortcode is called. In this example, we will create a simple function that will print “Hello World!” to the place where the shortcode is placed. You can change this text and function to be anything you like.

This code should be placed in your functions.php file.

//Function to echo Hello World!
function bloginbox_helloworld() {
echo "Hello world!";

How to create a shortcode

Now that we have the function that actually does something we can create the shortcode that executes the function every time the shortcode is called.

// shortcode to execute the function
add_shortcode('SHORTCODE_NAME_HERE', 'bloginbox_helloworld');

The shortcode is made of two parts. The first part “SHORTCODE_NAME_HERE” is the name we call our shortcode, so when placed on a WordPress page you would call it [SHORTCODE_NAME_HERE].

The second part is the function we want to execute when we call this shortcode. So if you would want to execute some other function than the one we just created, simply add it instead of the ‘bloginbox_helloworld’ to the shortcode.


In conclusion, if you want to create shortcodes to automate simple stuff on your WordPress, simply follow these three steps:

  1. Create the function
  2. Create the shortcode
  3. Add the shortcode to the post or page you want the code to be executed

Here is the full code one more time so you can simple copy and paste it to your functions.php to test it out:

// Function to echo Hello world!
function bloginbox_helloworld() {
echo "Hello world!";
// shortcode to execute the function
add_shortcode('SHORTCODE_NAME_HERE', 'bloginbox_helloworld');




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Experimenting with HTTP/3 — How the Next Gen Web is being built on UDP

What Is WordPress? Explained for Beginners. Technical Update 1

Waterfall-Agile Spectrum


The sane PHPMailer instruction “Sending message and files to the mail”

Wisdom for System Design

Apple M1, Python, Pandas, and Homebrew

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Blog Inbox

Blog Inbox

More from Medium

WordPress Boiler Plate Chase

How To Disable Emojis In WordPress

WordPress Emergency Support | Clean Commit

The 7 Best Simple and Minimal WordPress Themes for 2022