Creating custom shortcodes in WordPress

Need Fast, Reliable Web Hosting? Hosting.com Is The Leader In Optimized Hosting For Any Need! Try Our Hosting Service Today!

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.

👍

Tip

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');
  1. Click Update File. WordPress saves the changes to the functions.php file.

  2. 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

👍

Tip

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.

Related Articles