DEV Community

Cover image for Understanding Shortcodes in WordPress: A Beginner’s Guide
Jorge Araya
Jorge Araya

Posted on

Understanding Shortcodes in WordPress: A Beginner’s Guide

In the WordPress world, shortcodes are a very powerful feature. They allow you to insert complex functionality into your posts and pages with a simple code snippet. If you're just starting with WordPress development, this guide will help you understand what shortcodes are, how they work, and how to create your own!

What exactly are shortcodes?

Shortcodes are small pieces of code that you add to a post, page, or widget to include dynamic content. They act as placeholders that WordPress processes and replaces with more complex functionality when the page loads.

For example, [year] could be a shortcode used to display the current year dynamically.

Why should you use shortcodes?

They provide a simplified way to add advanced features to your content. Instead of writing the same code repeatedly, you can create a reusable shortcode to save time and maintain consistency. Shortcodes are perfect for adding custom features like contact forms, buttons, or galleries to multiple pages, with the ability to control them from a single place.

Now, let’s get to the good part: how to create custom shortcodes.

How to create a custom shortcode

Creating your own basic shortcode in WordPress requires just a little PHP.

1. Open your functions.php file.

2. Create a function for what you want the shortcode to do.

For example:

function cta_button_shortcode() {
    return '<a href="#" class="cta-button">Click Here!</a>';
}
Enter fullscreen mode Exit fullscreen mode

3. Register the shortcode.

The add_shortcodefunction takes two parameters: the first is the name of the shortcode, for example, [cta_button]. The second is the callback function that will handle all the logic and render the result—in this case, our cta_button_shortcode function.

add_shortcode('cta_button', 'cta_button_shortcode');
Enter fullscreen mode Exit fullscreen mode

4. Use Your Shortcode!

Now, you can use [cta_button] in any post or page, and it will display a link element with the text "Click Here!".

Adding parameters to the shortcode

You can also add parameters to shortcodes, which act as attributes that pass information to the shortcode function. This lets you change the text or the link of our button without needing to edit the code or create additional shortcodes. With parameters, you can also define default values in case they’re not provided in the shortcode.

I highly recommend checking out the official WordPress documentation about the shortcode atts.

See the example bellow:

function cta_button_shortcode($atts) {
    $atts = shortcode_atts(
        array(
            'text' => 'Click Here!', // Default value
            'url' => '/', // Default value
        ),
        $atts,
        'cta_button'
    );
    return '<a href="' . esc_url($atts['url']) . '" class="cta-button">' . esc_html($atts['text']) . '</a>';
}
Enter fullscreen mode Exit fullscreen mode

Now we can use it as [cta_button text="Learn More" url="https://example.com"], and if we need to change the text and link, we can do so without modifying our shortcode code.


Now you know the basics of WordPress shortcodes! With just a few lines of code, you can create powerful and flexible content elements that make your site more dynamic and easier to manage. Whether you’re building buttons, forms, or unique design elements, shortcodes are here to make your life as a developer a bit easier and a lot more fun.

So go ahead, experiment, and see what cool features you can create! 🎉 Shortcodes are like little magic tricks you can pull out anytime to make WordPress even more amazing. Happy coding! 🚀

Top comments (3)

Collapse
 
paulom profile image
cypherpaulo

Very helpful, thank you!

Collapse
 
jorgearay profile image
Jorge Araya

Thanks so much!!!

Collapse
 
kung_fu_panda_indian profile image
Shimanta Das

nice work!