+++ title = “Shortcodes” weight = 40 +++
While Markdown is good at writing, it isn't great when you need write inline HTML to add some styling for example.
To solve this, Gutenberg borrows the concept of shortcodes
from WordPress.
In our case, the shortcode corresponds to a template that is defined in the templates/shortcodes
directory or a built-in one.
Let's write a shortcode to embed YouTube videos as an example.
In a file called youtube.html
in the templates/shortcodes
directory, paste the
following:
<div {% if class %}class="{{class}}"{% endif %}>
<iframe
src="https://www.youtube.com/embed/{{id}}{% if autoplay %}?autoplay=1{% endif %}"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen>
</iframe>
</div>
This template is very straightforward: an iframe pointing to the YouTube embed URL wrapped in a <div>
.
In terms of input, it expects at least one variable: id
. Since the other variables
are in a if
statement, we can assume they are optional.
That's it, Gutenberg will now recognise this template as a shortcode named youtube
(the filename minus the .html
extension).
The markdown renderer will wrap an inline HTML node like <a>
or <span>
into a paragraph. If you want to disable that,
simply wrap your shortcode in a div
.
There are two kinds of shortcodes:
In both cases, their arguments must be named and they will all be passed to the template.
Lastly, a shortcode name (and thus the corresponding .html
file) as well as the arguments name
can only contain numbers, letters and underscores, or in Regex terms the following: [0-9A-Za-z_]
.
While theoretically an argument name could be a number, it will not be possible to use it in the template in that case.
Argument values can be of 5 types:
true
or false
.
in it.
in itMalformed values will be silently ignored.
Both type of shortcodes will also get either a page
or section
variable depending on where they were used and a config
one. Those values will overwrite any arguments passed to a shortcode so shortcodes should not use arguments called like one
of these.
Simply call the shortcode as if it was a Tera function in a variable block. All the examples below are valid calls of the YouTube shortcode.
Here is a YouTube video:
{{/* youtube(id="dQw4w9WgXcQ") */}}
{{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
An inline {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}} shortcode
Note that if you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
you will need to escape it by using {{/*
and */}}
instead of {{
and }}
.
For example, let's imagine we have the following shortcode quote.html
template:
<blockquote>
{{ body }} <br>
-- {{ author}}
</blockquote>
We could use it in our markup file like so:
As someone said:
{%/* quote(author="Vincent") */%}
A quote
{%/* end */%}
The body of the shortcode will be automatically passed down to the rendering context as the body
variable and needs
to be in a newline.
If you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
you will need to escape it by using {%/*
and */%}
instead of {%
and %}
. You won't need to escape
anything else until the closing tag.
Gutenberg comes with a few built-in shortcodes. If you want to override a default shortcode template,
simply place a {shortcode_name}.html
file in the templates/shortcodes
directory and Gutenberg will
use that instead.
Embed a responsive player for a YouTube video.
The arguments are:
id
: the video id (mandatory)class
: a class to add the div
surrounding the iframeautoplay
: whether to autoplay the video on loadUsage example:
{{/* youtube(id="dQw4w9WgXcQ") */}}
{{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
{{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}}
Result example:
{{ youtube(id="dQw4w9WgXcQ”) }}
Embed a player for a Vimeo video.
The arguments are:
id
: the video id (mandatory)class
: a class to add the div
surrounding the iframeUsage example:
{{/* vimeo(id="124313553") */}}
{{/* vimeo(id="124313553", class="vimeo") */}}
Result example:
{{ vimeo(id="124313553”) }}
Embed a player for a Streamable video.
The arguments are:
id
: the video id (mandatory)class
: a class to add the div
surrounding the iframeUsage example:
{{/* streamable(id="92ok4") */}}
{{/* streamable(id="92ok4", class="streamble") */}}
Result example:
{{ streamable(id="92ok4”) }}
Embed a Github gist.
The arguments are:
url
: the url to the gist (mandatory)file
: by default, the shortcode will pull every file from the URL unless a specific filename is requestedclass
: a class to add the div
surrounding the iframeUsage example:
{{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") */}}
{{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57", class="gist") */}}
Result example:
{{ gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57”) }}