While YouTube does makes it simple to embed a video into your website, the method doesn't come without its disadvantages - styles and features are limited, scalability isn't enabled by default, and changing site-wide occurrences of video presentations is problematic. This article introduces a WordPress shortcode that'll permit you to include single videos and playlists into a post or page with ease via the use of WordPress shortcode.
Video Demonstration
The following video introduces the basic usage of the shortcode and WP Plugin.
YouTube Embed Examples
Single Videos
To embed a single video with shortcode, use [youtube v="kVUy4i130iM"]
. Virtually any YouTube URL is accepted; we'll fetch the video ID. That said, it's best practice to include only the video ID. The result is as follows:
To play only a selected portion of a video, simply define the start and end time in seconds as follows: [youtube v="vbxl3hc_pL8" start="121" end="138"]
. The result is as follows:
A number of options can be applied, and they're listed below as "shortcode attributes".
Display a YouTube Video Playlist
A playlist can be a user created playlist, the results of a search, or user uploads. Additionally, you can provide your own comma separated list of video ID values.
To display a user created playlist - in this case one from Flight's channel - we'll use [youtube listtype="playlist" list="PLR6JWLm3S69cmRsglaYYGa7Jy0GDbsI44"]
. The result:
In another example of a playlist, this time for a search term, we'll use . Search playlists are no longer supported.[youtube listtype="search" list="space flight"]
. The result:
Display a Custom Playlist
A custom playlist includes a comma seperated list of any video of your choosing. For example, [youtube playlist="rE6a7uFJoss,Itjvfy2pBxA,NfQprOcjO3c"]
. The result:
If a primary video is defined (as youtube v="kVUy4i130iM playlist="rE6a7uFJoss,Itjvfy2pBxA,NfQprOcjO3c"]
, the defined video (in this case, kVUy4i130iMM
) will play before your playlist.
Shortcode Parameters
The following shortcode options are available. Many of the descriptions come from Google's player parameter page .
v
v
, or video ID, is required. To an embed a basic video it's the only attribute that's required.width
height
height
should almost always be left blank. Rather, we have a HD (true/false) option below that'll automatically scale the video as required.autoplay
autoplay="1"
the video will automatically play on page load. It's false by default.hd
hd="0"
. This attribute negates the need to specify a height.start
start
the video at a certain point, use start="75"
, where 75 is the number of seconds from the beginning of the video.end
end
the video at a certain point, use end="127"
, where 127 is the number of seconds from the beginning of the video.listtype
listtype
attribute is used in company with the list
attribute below. The listtype
should be set to playlist
, search
, or user_uploads
. The list below provides the value.list
list
parameter, in conjunction with the listType
parameter (above), identifies the content that will load in the player. If the listType
parameter value is user_uploads
, then the list parameter value identifies the YouTube channel whose uploaded videos will be loaded. If the listType
parameter value is playlist
, then the list parameter value specifies a YouTube playlist ID. If you specify values for the list
and listType
parameters, you do not need to specify a video ID. Note that search
is no longer supported.playlist
[youtube playlist="rE6a7uFJoss,Itjvfy2pBxA,NfQprOcjO3c"]
. They ID can be from any account and reference any video. If you also specify a video ID, that video will play first.privacy
privacy="1"
to use the www.youtube-nocookie.com
domain. Usually not required.modestbranding
modestbranding="0"
in your shortcode to enable the logo, or alter the shortcode function to avoid repeatedly having to include it.loop
loop="1"
.controls
controls
indicates whether the video player controls are displayed. If controls="0"
no player controls will be shown. If controls="1"
the controls load on page load, and if controls="2"
the controls appear with user interaction. by default, controls="1"
.rel
rel="1"
or alter the shortcode function if you'd like this feature enabled for all embeds.rel="1"
in your shortcode now forces related videos from your own channel - generally preferred behaviour. Set to rel="0"
to show videos from any channel.showinfo
cc_load_policy
cc_load_policy="1"
causes closed captions to be shown by default, even if the user has turned captions off. The default behavior is based on user preference. By default the value is 0 (false).color
color="white"
(this disables modest branding options).disablekb
disablekb="1"
(the spacebar and arrows control playback).fs
fs="0"
. The feature is enabled by default.hl
iv_load_policy
Options not discussed include enablejsapi
, origin
, and playsinline
. If you're looking for these features, you don't need us to explain them to you. More details on player parameter options are available here .
The Shortcode Function
It's always best to download the shortcode function rather than copy text. Alternatively, we've provided a WordPress plugin you can use like any other.
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Platform Usage
YouTube is heavily integrated into our Yabber Digital Marketing and Social Media platform. To set default behaviour for all videos globally, you may do so via the YouTube section of the Platform. In our case we simply store the default options and merge them with the $atts
array before returning a video.
Download
This snipped is due for an update and will be replaced soon.