Posted in: Newspaper
Dec 13, 2018 at 11:47 am
Hello there,
There is no schema.org support for featured videos. I had to add myself and I want to share it with you. Maybe you’ll include this in the future releases and maybe others find it useful. Thanks.
First of all I suggest using a child theme to accomplish this otherwise you have to alter theme files and these changes might get lost in the next update. Copy under from theme folder to your child theme includes/wp_booster/td_video_support.php file (with folder structure). Change the render method with the one below;
/**
* Render a video on the fornt end from URL
* @param $videoUrl - the video url that we want to render
*
* @return string - the player HTML
*/
static function render_video($videoUrl) {
$buffy = '<div class="wpb_video_wrapper">';
switch (self::detect_video_service($videoUrl)) {
case 'youtube':
$buffy .= '
<iframe id="td_youtube_player" width="600" height="560" src="' . 'https://www.youtube.com/embed/' . self::get_youtube_id($videoUrl) . '?enablejsapi=1&feature=oembed&wmode=opaque&vq=hd720' . self::get_youtube_time_param($videoUrl) . '" frameborder="0" allowfullscreen=""></iframe>
<script type="text/javascript">
var tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("td_youtube_player", {
height: "720",
width: "960",
events: {
"onReady": onPlayerReady
}
});
}
function onPlayerReady(event) {
player.setPlaybackQuality("hd720");
}
</script>
';
break;
case 'dailymotion':
$buffy .= '
<iframe frameborder="0" width="600" height="560" src="' . td_global::$http_or_https . '://www.dailymotion.com/embed/video/' . self::get_dailymotion_id($videoUrl) . '"></iframe>
';
break;
case 'vimeo':
$buffy = '
<iframe src="' . td_global::$http_or_https . '://player.vimeo.com/video/' . self::get_vimeo_id($videoUrl) . '" width="500" height="212" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
';
break;
case 'facebook':
/**
* cache & oembed implementation
*/
$cache_key = self::get_facebook_id($videoUrl);
$group = 'td_facebook_video';
if (td_remote_cache::is_expired($group, $cache_key) === true) {
// cache is expired - do a request
$facebook_api_json = td_remote_http::get_page('https://www.facebook.com/plugins/video/oembed.json/?url=' . urlencode($videoUrl) , __CLASS__);
if ($facebook_api_json !== false) {
$facebook_api = @json_decode($facebook_api_json);
//json data decode
if ($facebook_api === null and json_last_error() !== JSON_ERROR_NONE) {
td_log::log(__FILE__, __FUNCTION__, 'json decode failed for facebook video embed api', $videoUrl);
}
if (is_object($facebook_api) and !empty($facebook_api->html)) {
//add the html to the buffer
$buffy = $facebook_api->html;
//set the cache
td_remote_cache::set($group, $cache_key, $facebook_api->html, self::$caching_time);
}
} else {
td_log::log(__FILE__, __FUNCTION__, 'facebook api html data cannot be retrieved/json request failed', $videoUrl);
}
} else {
// cache is valid
$api_html_embed_data = td_remote_cache::get($group, $cache_key);
$buffy = $api_html_embed_data;
}
break;
case 'twitter':
/**
* cache & oembed implementation
*/
$cache_key = self::get_twitter_id($videoUrl);
$group = 'td_twitter_video';
if (td_remote_cache::is_expired($group, $cache_key) === true) {
// cache is expired - do a request
$twitter_json = td_remote_http::get_page('https://publish.twitter.com/oembed?url=' . urlencode($videoUrl) . '&widget_type=video&align=center' , __CLASS__);
if ($twitter_json !== false) {
$twitter_api = @json_decode($twitter_json);
//json data decode
if ($twitter_api === null and json_last_error() !== JSON_ERROR_NONE) {
td_log::log(__FILE__, __FUNCTION__, 'json decode failed for twitter video embed api', $videoUrl);
}
if (is_object($twitter_api) and !empty($twitter_api->html)) {
//add the html to the buffer
$buffy = $twitter_api->html;
//set the cache
td_remote_cache::set($group, $cache_key, $twitter_api->html, self::$caching_time);
}
} else {
td_log::log(__FILE__, __FUNCTION__, 'twitter api html data cannot be retrieved/json request failed', $videoUrl);
}
} else {
// cache is valid
$api_html_embed_data = td_remote_cache::get($group, $cache_key);
$buffy = $api_html_embed_data;
}
break;
}
$buffy .= sprintf( '
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "VideoObject",
"name": "%1$s",
"description": "%2$s",
"thumbnailUrl": "%3$s",
"contentUrl": "%4$s"
}
</script>',
esc_attr( get_the_title() ),
esc_attr( get_the_excerpt() ),
get_the_post_thumbnail_url( get_the_ID(), 'featured' ),
$videoUrl
);
$buffy .= '</div>';
return $buffy;
}
For better indentation and readability see: https://pastecode.xyz/view/399f6207
-
This topic was modified 7 years by
emreerkan.
Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newspaper’ is closed to new topics and replies.