I trust that you found this blog post to be enjoyable. If you are interested in having my team handle your eCommerce setup and marketing for you, Contact Us Click here.

How to Create a Shortcode in WordPress (Beginner's Guide)

How to Create a Shortcode in WordPress (Beginner's Guide)

WordPress shortcodes are an effective way to give your posts, pages, and widgets unique functionality. They allow you to execute code inside WordPress content without entering any code in the content editor. In this article, we'll guide you through constructing a custom shortcode in straightforward stages, complete with an example using ob_start to capture and return dynamic content.

What is a Shortcode?

Enclosed in square {} brackets, a shortcode—a little piece of code—can dynamically add unique features or functionalities into PHP content sections (posts, pages, or widgets). When PHP hits across a shortcode, it substitutes the output of the related function for the shortcode tag.

The shortcode [gallery] for instance displays a photo gallery, and [contact-form] adds a contact form.

Why Do You Need Shortcodes?

Shortcodes find application for multiple functions.

  • Ease of Use: There is no coding experience needed; they help you to include advanced capability into your website. Just put the shortcode tag into use.
  • Reusability: Shortcodes prevent code duplication using the same feature across many pages or posts.
  • Clean Code: Keeping your content editor neat and tidy helps you to manage your materials more easily.
  • Flexibility: Shortcodes allow dynamic content—forms, galleries, customised greetings, even data from an outside source—to be incorporated.

Step 1: Create a Function for the Shortcode:

We first have to design a mechanism to produce the shortcode content. Any PHP code you need to generate the intended output can be included into this feature. Here, we will design a basic function displaying a personalised message.

 

<?php

function custom_shortcode_function() {

    ob_start();

    ?>

    <div class="custom-message">

        <h2>Welcome to My WordPress Site!</h2>

        <p>This is a custom message created by a shortcode.</p>

    </div>

    <?php

  

    return ob_get_clean();

}

?>

 

Step 2: Register the Shortcode

We next have to register the shortcode with PHP using the add_shortcode feature on WordPress. This feature links our unique capability with a certain shortcode tag.

function register_custom_shortcode() {

    add_shortcode('custom_message', 'custom_shortcode_function');

}

add_action('init', 'register_custom_shortcode');

Here, we register a shortcode [custom_message] that will execute the custom_shortcode_function when used.

 

Complete Example Code

This is the entire code to add to a functions.php file in your theme or a custom plugin file to integrate everything.

 

Step 3: Use the Shortcode in Your Content

You can use the shortcode anywhere in your WordPress text once it has been registered. To add the shortcode tag, just add it to any page, post, or widget.

[custom_message]

When WordPress encounters this shortcode, it will execute the custom_shortcode_function, capture the output using ob_start, and insert the generated HTML content in its place.

Step 4: Utilise the PHP file's shortcode

Shortcodes can be used simply in PHP files that compose your theme or plugin as well as directly in PHP content such posts, pages or widgets. This is quite useful if you want to add plugins' functionality or dynamic content into your theme templates.

The do_shortcode tool will help you to run the PHP shortcode and produce the outcome.

To do this, you use the do_shortcode function in PHP. Here's an example of how to use it:

In summary

WordPress shortcode creation is a simple procedure that can greatly increase the usefulness and flexibility of your website. You can quickly collect and return dynamic content produced by your PHP code by using ob_start. See how this shortcode can help you organize your content development process by adding it to your WordPress website. I've created a shortcode in this example to show personalized messages only. But you can create a shortcode for any feature you need, like displaying a certain area, adding dynamic content, or displaying an additional personalized message. You can reuse and easily manage features on your WordPress website by creating your own shortcodes.

Back to blog