GeckoGrafix
10/29/2018 - 5:47 PM

Embed media Shortcode

/*
	Embed YouTube Videos in WordPress With Shortcode
	http://www.beliefmedia.com/youtube-videos-wordpress-shortcode
	https://developers.google.com/youtube/player_parameters
*/
 
 
function beliefmedia_embed_youtube_player($atts, $content = null) {
 extract(shortcode_atts(array(
  'v'  => 0,
  'width'  => '560',
  'height' => '', /* leave blank */
  'frame' => '0',
  'autoplay' => 0,
  'hd' => 1, /* 16: 9, SD 4:3 */
 
  'start' => 0, /* Seconds from the start of the video */
  'end' => 0, /* End video 'seconds' from start */
 
  'list' => 0, /* ID of list, used in company with listType */
  'listtype' => 0, /* playlist, search, and user_uploads */
  'playlist' => 0, /* comma-separated list of video IDs to play AFTER first defined video */
 
  'privacy' => 0,
  'modestbranding' => 1, /* 0 - show YouTube branding */
  'loop' => 0, /* 1 - loops video */
  'controls' => '1', /* 0 (no controls), 1 (controls pre-load, default), or 2 */
  'rel' => '0', /* Set to 1 to show related video when vid ends */
  'showinfo' => 1, /* Show video title & uploader before starts playing */
  'autohide' => '2', /* 1 - progress bar/controls hides auto, 0 - progress bar/controls hidden (flash only) */
  'cc_load_policy' => 0, /* 1 forces closed captions to display by default */
 
  'color' => 1, /* 1 - red (default) 0 - white :: color progress bar */
  'theme' => 0, /* Using 1 sets theme to 'light' (disables modestbranding) :: deprecated */
  'disablekb' => 1, /* 1 - enables keyboard shortcuts */
  'enablejsapi' => 0, /* 1 enables player controlled via IFrame or JS API calls */
  'fs' => 1, /* 0 disables full screen option */
  'hl' => '', /* Interface language http://www.loc.gov/standards/iso639-2/php/code_list.php */
  'iv_load_policy' => '3', /* 1 - video annotations shown by default */
  'origin' => 0, /* If using JS API, sets URL as origin */
  'playsinline' => 0 /* play inline or fullscreen in an HTML5 player on iOS */
 
), $atts));
 
/* Determine height of video */
if (!$height) $height = ($hd) ? '56.25%' : '75%';
 
$style = '<style scoped>
.auto-resizable-iframe {
  max-width: ' . $width . 'px;
  margin: 0px auto;
}
 
.auto-resizable-iframe > div {
  position: relative;
  padding-bottom: ' . $height . ';
  height: 0px;
}
 
.auto-resizable-iframe iframe {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
}
</style>';
 
  /* Get video ID from $v (if required) */
  if ( ($v) && (strpos($v,'http') !== false) ) {
   preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\??n]+|(?<=youtu.be/)[^&\n]+#", $v, $matches);
   $v = $matches['0'];
  }
 
 /* Playlist or video? */
 if ($playlist) {
   $v = ($v) ? '/' . $v . '?playlist=' . $playlist : '?playlist=' . $playlist;
  } else {
   if ($listtype == "search") $list = str_replace(' ', '+', $list);
   $v = ($listtype) ? '?listType=' . $listtype . '&list=' .  $list = ($listtype == "playlist") ? 'PL' . $list : $list : '/' . $v;
 }
 
 /* Determine if we embed the privacy URL */
 $domain = ($privacy) ? 'youtube-nocookie' : 'youtube';
 
 /* Return embed code ::  */
 $return = '<p><div id = "scoped-content">' . $style;
 $return .= '<div class="auto-resizable-iframe"><div><iframe id="ytplayer" type="text/html" src="http://www.' .$domain . '.com/embed' . $v;
 $return .= ( ($listtype) || ($playlist) ) ? '&rel=' . $rel : '?rel=' . $rel;
 if ($start) $return .= '&start=' . $start;
 if ($end) $return .= '&end=' . $end;
 if ($modestbranding) $return .= '&modestbranding=1';
 if ($loop) $return .= '&loop=1';
 if ($controls != '1') $return .= '&controls=' . $controls;
 if (!$showinfo) $return .= '&showinfo=0';
 if ($autohide != '2') $return .= '&autohide=' . $autohide;
 if ($cc_load_policy) $return .= '&cc_load_policy=1';
 if (!$color) $return .= '&color=white';
 if ($theme) $return .= '&theme=light';
 if (!$disablekb) $return .= '&disablekb=0';
 if ($enablejsapi) $return .= '&enablejsapi=1';
 if (!$fs) $return .= '&fs=0';
 if ($hl != '') $return .= '&hl=' . $hl;
 if ($iv_load_policy != '3') $return .= '&iv_load_policy=' . $iv_load_policy;
 if ($playsinline) $return .= '&playsinline=' . $playsinline;
 $return .= '&origin=' . site_url(); 
 $return .= '" frameborder="' . $frame . '" allowfullscreen></iframe></div></div></div></p>';
 
return $return;
}
add_shortcode('youtube', 'beliefmedia_embed_youtube_player');
v
For an individual video, the v, or video ID, is required. To an embed a basic video it’s the only attribute that’s required.

width
The shortcode is essentially ready to use. The only option that you’ll be required to change in the shortcode function that you download is the width. This should be set to style with the width of your post container.

height
The 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
If autoplay="1" the video will automatically play on page load. It’s false by default.

hd
By default we’ll display the standard widescreen video container (16:9). If you’re embedding standard definition video, use hd="0". This attribute negates the need to specify a height.

start
To start the video at a certain point, use start="75", where 75 is the number of seconds from the beginning of the video.

end
To end the video at a certain point, use end="127", where 127 is the number of seconds from the beginning of the video.

listtype
The 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
The list parameter, in conjunction with the listType parameter (above), identifies the content that will load in the player. If the listType parameter value is search, then the list parameter value specifies the search query. 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.

playlist
To render a custom playlist, you do so with comma separated video id values as follows: [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
Use privacy="1" to use the www.youtube-nocookie.com domain. Usually not required.
modestbranding

This URL parameter lets you use a YouTube player that does not show a YouTube logo. By default we don’t show it. Use modestbranding="0" in your shortcode to enable the logo, or alter the shortcode function to avoid repeatedly having to include it.

loop
To loop a single video over and over, use loop="1".

controls
The 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
By default we don’t show the related videos at the end of your video. If you would like to enable this feature, use rel="1" or alter the shortcode function if you’d like this feature enabled for all embeds.

showinfo
Setting the parameter’s value to 0 causes the player to not display information like the video title and uploader before the video starts playing. Enabled by default.

autohide
This is a flash specific parameter so can generally be ignored. It indicates whether the video controls will automatically hide after a video begins playing.

cc_load_policy
Setting 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
This parameter specifies the color that will be used in the player’s video progress bar to highlight the amount of the video that the viewer has already seen. Valid parameter values are red and white, and, by default, the player uses the color red in the video progress bar. To use the white progress bar, use color="0".

disablekb
By default keyboard shortcuts are disabled. To enable, use disablekb="1" (the spacebar and arrows control playback).

fs
To disable the ‘full screen’ option, use fs="0". The feature is enabled by default.

hl
You can optionally provide a interface language . If this were required it would be best to do it via the actual function rather than using it in your shortcode on every occasion.

iv_load_policy
Setting the parameter’s value to 1 causes video annotations to be shown by default, whereas setting to 3 causes video annotations to not be shown by default. Our default value is 3 (disabled).