Height-Challenged Code (Shortcodes) in Silverstripe

Using the native shortcode system in Silverstripe
Published on Jan 16 2015 by Tim Larsen
What is a shortcode?

A shortcode is essentially a snippet of text like [shortcode] that is a shortcut to a block of code written by an awesome developer.

Why are they useful?

Shortcodes are useful for giving non-technical users the ability to access technical functionality or just to simplify the process of adding sophisticated content.

I've heard of shortcodes in WordPress - are they the same thing?

From an end-user perspective; yeah, pretty much.
From a developer's perspective; there's a bit more work involved with integrating a shortcode in SilverStripe, but you also get a lot more power. We'll dive in to this a bit more in the followup.

How do I use shortcodes in SilverStripe?

SilverStripe has made the process of using shortcodes super simple. As long as your template has been developed correctly, there will be no need to touch any code. From the CMS, simply edit a page and insert the desired code in to the content region. If that doesn't work, ask your developer to integrate formatted code in to your template.

What shortcodes are built in to SilverStripe?

SilverStripe actually comes with a couple of shortcodes that you can try right now:

Shortcode text: [sitetree_link,id=33] 
Description: Pulls in the url of a chosen page in your SiteTree. This is better than manually entering a URL, as your SiteTree structure may change, breaking any hard-coded URLs. This can also be added in the WYSIWYG by selecting text and clicking the Insert Link icon.
Example:  /blog/tutorials/shortcodes-in-silverstripe/

Shortcode text: [file_link,id=177]
Description:  Same as above, but with files.
Example: /assets/Uploads/shortcode.jpg

Shortcode text: [embed width=40 height=40]https://www.youtube.com/watch?v=YrblkMd-KMs[/embed]
Description: Use the OEmbed functionality to embed some external content. See below for more info.
Example: 

Side note: while the official documentation states you should use a comma to separate values in a shortcode, spaces work as well - and using comma's in the [ embed] tag actually lead to an error in the SilverStripe CMS.

I'm sure there are other shortcode that are usable in SilverStripe, but from what I can see, the official documentation at the moment is a little bit lacking.

OEmbed 

SilverStripe is awesome enough to have built-in embedding functionality for the following sources:

  • YouTube
  • Flickr
  • Viddler
  • Revision3
  • Hulu
  • Vimeo
  • Twitter

I'll add a short tutorial on how we can add support for other providers in the future.

This means that, for any of the above providers, we can use their links in between tags (like in the example above) to pull in content easily.

How do I add more shortcodes to SilverStripe?

There are a bunch of ready-made SilverStripe modules that provide extra functionality available at the SilverStripe add-on directory

Alternatively, if you'd like to build your own Shortcode you can take a look at the follow-up technical article on "Creating Shortcodes" that will be live in the next couple of weeks.