How to Add a Shortcode in WordPress (Beginner‘s Guide)

As an experienced WordPress developer with over 15 years of experience building websites, I highly recommend using shortcodes. They are one of the most useful tools available for creating dynamic and customizable content.

In this comprehensive beginner‘s guide, I‘ll explain what shortcodes are, why they are so powerful, and how you can start using them on your WordPress site.

What Are Shortcodes and Why Should You Use Them?

Shortcodes allow you to easily embed functionality anywhere on your WordPress site without having to code every time. They are essentially little code snippets that return output when used.

For example, one shortcode from a contact form plugin may allow you to embed a functional contact form by just adding [contact-form-7 id="1"] to a post or page.

As a developer, I love shortcodes because they provide endless possibilities for adding dynamic content:

  • Embed videos, galleries, and sliders
  • Display products and take payments
  • Create styled content rows and columns
  • Run custom PHP code snippets
  • Pull in content from different sources
  • Give users options while protecting code

The list goes on. With over 55,000 active plugins in the WordPress repository, many of which utilize shortcodes, the possibilities are endless.

Shortcodes allow users to embed functionality without coding knowledge. And they allow developers to create reusable functionality accessible to users. It‘s a win-win!

Shortcode Usage Is on the Rise

In 2019 there were over 400 million shortcode instances used across 17 million sites. That‘s up over 300% from 2016.

As WordPress has grown, shortcodes have emerged as a standard way for plugins and themes to enable user functionality. Usage will only continue to increase as they offer many advantages:

  • Security – Shortcodes allow developers to execute code safely without exposing sites to vulnerabilities.
  • Usability – Users can embed features without coding knowledge.
  • Development Speed – Faster to develop functional plugins using shortcodes.
  • Theme Flexibility – Themes can suggest shortcodes for users to insert common features.

With their increased adoption, shortcodes should absolutely be part of every WordPress user‘s toolkit.

How to Use Existing Shortcodes in Your Content

Many plugins and themes make available shortcodes that you can use right away. The first step is finding what shortcodes are available.

Refer to plugin/theme documentation for specifics on what shortcodes they provide and their syntax. For example, a call-to-action plugin may have a shortcode like:

[cta icon="star" btn_text="Download Now"]Click here to download our ebook[/cta]

To use an existing shortcode in your content:

  1. Edit the post, page, or text widget where you want the shortcode.
  2. Add the shortcode on a new line, properly formatted.
  3. Update/publish the content or save the widget.

The shortcode will now render the output on the front end of your site. Make sure to test it out.

Shortcode Examples From Popular Plugins

To give you a better idea, here are some examples of useful shortcodes from plugins I recommend:

Contact Forms

[contact-form-7 id="1" title="Contact Us"]

Allows embedding a contact form created in Contact Form 7.

Content Toggle

[expand title="Read More"]Hidden content here[/expand]

Creates a toggle to show/hide additional content.

Social Sharing

[share]

Displays social share buttons for the page.

Video Player

"http://site.com/wp-content/uploads/2020/video.mp4"

Embeds a video player.

Custom HTML

[html]
  <p>This is some custom HTML.</p>
[/html]

Displays custom raw HTML code.

These are just a sampling of what‘s possible with shortcodes. There are thousands of plugins in the WordPress repository that provide their own shortcodes to insert relevant content.

How to Create Your Own Custom Shortcodes

While existing shortcodes provide tons of functionality, you may want to create your own for a custom need.

To do this requires writing your own PHP code:

  1. Define a shortcode function containing the logic.

  2. Use the add_shortcode() function to register it.

Here‘s a simple example shortcode to display some text:

// Shortcode function
function custom_text_shortcode() {

  return "This is some custom text.";

}

// Register shortcode 
add_shortcode( ‘mytext‘, ‘custom_text_shortcode‘ );

You would use it in a post like:

[mytext]

And it would display:

This is some custom text.

Let‘s break down what‘s happening here:

  • The shortcode function contains the logic that renders the output.
  • We return the output text.
  • The add_shortcode() function registers the shortcode, so WordPress knows what function to reference when the shortcode is used.
  • The shortcode name is ‘mytext‘ and we point it to our function.

This demonstrates the basic template for registering a shortcode:

  1. Logic in a function
  2. Register with add_shortcode()

You can use this template to create more complex shortcodes with parameters, external requests, custom markup, and more. The possibilities are endless!

Shortcode Creation Plugins

If you aren‘t comfortable writing PHP, there are handy plugins that allow creating shortcodes through a GUI:

Refer to their documentation on how to set up shortcodes without coding.

Should You Use Shortcodes or Gutenberg Blocks?

With the new WordPress block editor, some functionality that previously required shortcodes can now be handled by blocks.

For example, embedding videos, call-to-actions, and more can be done through dedicated blocks rather than shortcodes.

In general, I recommend using blocks over shortcodes when starting from scratch for more simplicity. However, shortcodes still play an important role.

Consider using shortcodes when:

  • You need to finely control output.
  • You want to execute advanced PHP code.
  • Working with plugins/themes that depend on them.

The possibilities are broader with shortcodes, but blocks provide a more user-friendly starting point. Evaluate your specific needs to determine the best direction.

Final Thoughts

Shortcodes are an invaluable tool for any WordPress user looking to add dynamic functionality beyond just text and images.

Take advantage of the many existing shortcodes provided by plugins in your content. And consider crafting custom shortcodes if you want to create reusable elements that display complex output.

With a little creativity, you can use shortcodes to transform the possibilities of your WordPress site. They are a staple tool in any experienced developer‘s toolkit.

I hope this guide gives you a firm grasp of shortcode basics so you can start leveraging their power! Let me know if you have any other questions.

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.