How to Create Custom Shortcodes in WordPress in 2023

Create Custom Shortcodes in WordPress in 2023

In this blog post, we will discuss how to create custom shortcodes in WordPress and use them to enhance your website’s functionality.

Shortcodes are an essential part of WordPress development. They allow developers to create reusable code snippets that can be easily inserted into posts, pages, and other content areas using a simple shortcode. While WordPress comes with many built-in shortcodes, you may need to create custom ones for your website.

Step 1: Decide what your shortcode will do

Before creating a shortcode, it’s essential to determine what it will do. A shortcode can do anything, from displaying a simple text message to executing complex PHP code. Here are some ideas for custom shortcodes:

  • Display a contact form
  • Embed a video
  • Display a table of contents
  • Display related posts

Step 2: Create the shortcode function

Once you have decided what your shortcode will do, you need to create a function that will execute the shortcode. This function should be added to your theme’s functions.php file or a custom plugin.

Here’s an example of a simple shortcode function that displays a message

function custom_shortcode() {
    return 'Hello, World!';
add_shortcode('custom', 'custom_shortcode');

In the code above, we have created a function called “custom_shortcode” that returns the message “Hello, World!”. We have also used the “add_shortcode” function to register the shortcode with the name “custom”.

Step 3: Use the shortcode in your content

Now that you have created the shortcode, you can use it in your content. To do this, simply add the shortcode to any post or page like this:


When the post or page is viewed, the shortcode will be replaced with the output of the function we created earlier.

Step 4: Add shortcode attributes

Shortcodes can also have attributes that allow you to customize their output. For example, you may want to display a different message depending on the attribute values.

Here’s an example of a shortcode function that uses attributes:

function custom_shortcode_with_attributes($atts) {
    $atts = shortcode_atts(array(
        'message' => 'Hello, World!'
    ), $atts);
    return $atts['message'];
add_shortcode('custom', 'custom_shortcode_with_attributes');

In this example, we have created a function called “custom_shortcode_with_attributes” that accepts an array of attributes. We have also used the “shortcode_atts” function to set default values for the “message” attribute. Finally, we have returned the value of the “message” attribute.

To use this shortcode with attributes, you would add it to your content like this:

[custom message="Welcome to my website!"]

When the post or page is viewed, the shortcode will be replaced with the value of the “message” attribute, which in this case is “Welcome to my website!”.


Creating custom shortcodes in WordPress is a great way to enhance your website’s functionality and make your content more engaging. By following the steps outlined in this blog post, you can create custom shortcodes that do just about anything. Remember to test your shortcodes thoroughly before using them on a live website, and don’t be afraid to experiment with different shortcode functions and attributes until you find the perfect solution for your needs.