Custom Shortcodes for WordPress

Originally published at:

What Are Shortcodes?

Shortcodes are a convenient method that WordPress implements which provides a way to generate dynamic and often complex functionality from very little input.

While developers often create functionality to provide additional features for a website; they are executed by calling PHP code. Users however, are often not developers, and as such having them interact directly with PHP is not recommended. A solution to this is for developers to create custom shortcodes.

Shortcodes provide a level of dynamic content for the user, allowing them to create power features on their website by using pseudo-code that act similar to macros. The shortcode when called will take its arguments (if any) and then run through a function which defines what custom functionality will occur.

For example, you may have a basic shortcode that is wrapped around your links to create a pretty button: [[pretty_button]] Read More! [[/pretty_button]]

Who Creates Shortcodes?

Shortcodes have to be registered on your website in order for them to be used. There are several ways in which shortcodes can be added to your website, here are a few common methods.

Added by WordPress Itself

WordPress actually comes with a series of shortcodes already defined. These shortcodes are generally used for galleries, captions, audio and video elements and mostly related to the media side of WordPress.

For example, you may want to display an inline video player on your page. By using the [video] shortcode as follows you can [video src="video-source.mp4"]

There are several shortcodes that are documented on the WordPress codex page

Inbuilt into a Theme

A lot of theme developers will provide a series of shortcodes that you can use to add a lot of extra functionality to your website. These shortcodes may create interactive elements, display content in visually stunning ways or be very informational.

When purchasing themes or using popular free themes, the developer will often provide a page on their website that outlines their included shortcodes (along with how to use them and examples). In addition, the theme even may include a shortcode button on the content editor (which allows a simple way to insert your shortcode)

However, a theme may also have no extra shortcodes at all.

Comes with a Plugin

Most plugins that offer some sort of content (such as forms, sliders, interactive elements) will come with a shortcode that you either configure on a case by case basis (you add the options inside the shortcode on the desired page) or is configured globally (inside a plugins theme menu for example).

These plugin shortcodes often are well documented on the developers website or the plugin repository for WordPress, giving you an instant idea of what exactly you can do with your shortcode.

For example, you may want to show your product cart on a WooCommerce powered website, you could do this by using their cart shortcode [[woocommerce_cart]]. These shortcodes are outlined on their shortcode documented page.

Continue reading this article on SitePoint
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.