How to create custom shortcodes in WordPress

This article describes how to create and use custom shortcodes in WordPress.

Shortcodes are useful when you have to add dynamic or repeatable content to multiple areas of a WordPress site. By editing a shortcode, you can quickly change content in multiple areas of your site simultaneously, instead of editing multiple pages individually.

WordPress includes several built-in shortcodes. To view a list of these shortcodes, please visit https://wordpress.com/support/shortcodes.

Creating and using a shortcode

To create and use a shortcode in WordPress, follow these steps:

  1. Log in to WordPress as the administrator.
  2. On the Dashboard in the left sidebar, click Appearance, and then click Theme Editor:

    WordPress - Dashboard - Appearance - Theme Editor

  3. In the right sidebar, under Theme Files, click Theme Functions (functions.php).
  4. Copy the following code snippet and then paste it at the bottom of the functions.php file:

    function custom_shortcode() {
        // Change $message to the text you want to display:
        $message = 'Here is the shortcode text.';
    
        return $message;
    } 
    
    add_shortcode('my_custom_shortcode', 'custom_shortcode');
    
  5. Click Update File. WordPress saves the changes to the functions.php file.
  6. The shortcode is now enabled. To use it, open a post or page on the site, and then type [my_custom_shortcode] where you want to display the shortcode message text. For example:

    WordPress - Custom shortcode example

    Save your changes, and then view the post:

    WordPress - Custom shortcode example

    You can create as many shortcodes as you want, as long as they have unique names in the functions.php file.

More Information

For more information about shortcodes in WordPress, please visit https://wordpress.com/support/shortcodes.

Get WordPress Hosting

Article Details

Other Articles in This Category

Show More

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.