The seventh and final instalment in our shortcode series deals with the [ video ] shortcode. This shortcode is used to easily play videos in a page or post, whether those videos are included in your WordPress media library or hosted externally. The code is as easy to use as the [ audio ] shortcode discussed in the second article of this series.
The previous articles discussed of the following:
- Introduction to Shortcodes
- Audio Shortcode
- Caption Shortcode
- Embed Shortcode
- Gallery Shortcode
- Playlist Shortcode
In its simplest form, you would place [ video ] wherever you want an embedded player to start showing your video. You can use the shortcode for one or multiple videos as needed.
Using the [ video ] Shortcode
As with all of the other shortcodes discussed in this series, the [ video ] shortcode is easy to use. Just enter [ video ] followed by any other additional options you wish to invoke. Here is an example taken directly from the WordPress documentation:
[ video mp4="source.mp4" ogv="source.ogv" webm="source.webm" ]
In this example, you can see that three different videos have been chosen for play. You will also notice different file formats. This is because the code supports six of the most popular formats used on the web today.
The [ video ] shortcode supports the following options as either strings or integers (note the last two are required; if you fail to set them, defaults will be used):
- src – This string defines the source of your videos if you want to rely on any files not attached to the page or post. If you do not set this option, the player will default to the first video attached to your page or post.
- poster – This option makes it possible to display a placeholder until videos start playing.
- autoplay – With this option, you can direct the media player to begin automatically playing videos as soon as the media is ready.
- preload – This option defines the loading parameters for your videos. You can choose ‘metadata’, ‘none’, or ‘auto’. The metadata setting tells the player to auto load metadata with the page; ‘none’ prevents automatic loading when the page loads; ‘auto’ tells the player to begin loading all videos along with the page.
- loop – Just as the name implies, this option allows you to loop your videos for continuous play. The default is ‘off’.
- height – This is the first of the required integers used to define the height of the media player. If you don’t define it, the player will automatically adjust according to the first video to be played.
- width – This is the second required integer that works exactly like height, except that it determines the width of the player. Be advised that the width of the player can never exceed the maximum width of content as determined by your WordPress theme.
You should now have a basic understanding of how WordPress shortcodes work. It is good knowledge to have, given the fact that theme developers and plugin authors tend to create their own shortcuts for ease-of-use purposes. Shortcodes are a great way to add functionality to WordPress.