WordPress shortcodes were introduced in version 2.5 of the WordPress framework, and have since proved to be one of the most popular and powerful features. Think of their use as a "find and replace" (macro) feature where static text (formatted with
[squarebrackets] in your post editor, or where you write your content) is replaced by dynamic content - such as recent posts, videos, images, or virtually any other kind of data.
Note, 4th November 2020: As a result of shortcode changes some of the demonstration shortcodes on this page may not render as expected.
We'll be publishing countless shortcodes on the BeliefMedia website. If you're interested in trying them out you'll need to consider installing our custom functions WP plugin. The plugin will essentially be a repository for the code we provide.
Because shortcode snippets can be quite short they'll often negate the need for a plugin while, at the same time, providing as much - if not more - functionality. Their use means that your WordPress installation will be more efficient because it isn't carrying the overhead associated with bloated or superfluous plugin code. While plugins will always be required for more sophisticated or complex requirements (the WP framework relies on plugins to extend their features), the cut-and-paste shortcode functions are often an efficient replacement for plugins that provide simple features.
The easiest means of explaining what shortcode does is to provide a few examples.
Creating Links with our SHORTT Shortcodes
All the links on this (and all our client websites) are created with shortcodes. Instead of standard HTML code, we'll create a link as follows:
[link url="http://www.flight.org"]. The result automatically provides a title if one isn't defined and adds an icon associated with the outgoing website. The result: Flight . If we're linking to our internal contact page we'll use
[link url="117"]contact[/link] with a result as follows: contact. If we wanted to truncate a URL for tracking purposes (in this case using our shor.tt URL service), we'll use
[link url="http://www.flight.org" shortt="1"]Flight[/link] with a result of Flight (note it's now a shor.tt URL). The same linking applies to categories , tags , and unpublished posts - all providing descriptive images and relevant title tags.
While creating SEO-optimized links are simple examples - all of which we urge our client to use (as a plugin) - they do provide a basic example of shortcode usage.
A good example of shortcode usage comes from embedding YouTube videos in WordPress posts or pages (See: "Embed YouTube Videos in WordPress With Shortcode"). Before we continue, consider the disadvantages of posting YouTube's default embed code into a WordPress website (we cover this in more detail in our scheduled article).
- WordPress' editor can often cause unpredictable results when pasting code of any type - particularly when using the visual editor.
- If you change WordPress themes or alter the existing design of your website (or simply alter the width of all your video containers), you'll have to alter every occurrence of the YouTube embed code on your website to ensure that they render correctly.
- If YouTube alters their embed code (as they have done in the past), your embeds may cease to work.
- If YouTube adds another feature to their video presentation and you want to make the same change to your videos, you'll have to edit every occurrence of the embed code on your website.
You can navigate the potential pitfalls as described above by using a simple line of shortcode. For example, to render a single video we'll simply use
[youtube v="kVUy4i130iM"]. The result:
We can provide a start and stop time as follows:
[youtube v="vbxl3hc_pL8" start="52" end="95"]. The result:
We can optionally define playlists, user uploads, searches, or a string of custom and unrelated videos.
You may note that the two videos above don't show the YouTube branding or trailing related videos. This behavior is coded into the shortcode by default, but you could easily edit the shortcode function and alter the presentation of every video you post. Behavior such as looping the video and disabling full screen playback are detailed alongside about a dozen different options in our upcoming article.
Another scheduled post provides a feature to count the number of scheduled, draft, or published posts by a specific author or all contributors to your WordPress website. For example, to display the total number of published articles on this website we'll use the shortcode of
[numberposts]. The result: 437. Using this particular snippet of shortcode we can further count drafts and scheduled posts, and limit the result returned by author or timeframe.
oEmbed and WordPress
WordPress generally supports oEmbed - often considered a poor-man's alternative to shortcode. oEmbed describes itself as "a format for allowing an embedded representation of a URL on third party sites... the simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly." The oEmbed functionality in WordPress will take a single line of text (say, a YouTube URL) and, assuming the provider is registered on WP, make an attempt to covert that link into dynamic content. While not completely unlike shortcodes (in that they provide a similar find-and-replace result), we avoid them because they're notoriously unpredictable and unreliable (more of a WordPress problem than an oEmbed problem). More importantly, use of oEmbed provides virtually no customization options.
Belief Media's Shortcodes
For most of the shortcodes we provide on this website we'll also provide a plugin that can be uploaded and activated like any other. That said, if we provide a shortcode snippet it's usually because it makes more sense. The latter option is generally provided for those that aren't interested in digging into the code that makes WordPress work.