最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

YouTube oEmbed and privacy-enhanced mode

programmeradmin0浏览0评论

When you´re embed a youtube video in an iframe, you can enable the privacy-enhanced mode, so YouTube doesn´t store information about your web page visitors until they play the video.

I've tried to embed a video via oEmbed and the URL

but it didn't work. Is there a chance to implement a privacy-friendly solution with oEmbed?


EDIT I found this proposal and tried to customize it and it seems to work, but there is one thing that is not optimal. You can´t use the defined $content_width, because this solution needs a declaration of the height, too. Any ideas to this approach or do you have another?

 wp_embed_register_handler( 'ytnocookie', '#https?://www\.youtube\-nocookie\/embed/([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal', '#https?://www\.youtube\/watch\?v=([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal2', '#https?://www\.youtube\/watch\?feature=player_embedded&v=([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );

 function wp_embed_handler_ytnocookie( $matches, $attr, $url, $rawattr ) {
   global $defaultoptions;
   $defaultoptions['yt-content-width'] = '680';
   $defaultoptions['yt-content-height'] = '510';
   $defaultoptions['yt-norel'] = 1;
   $relvideo = '';
   if ($defaultoptions['yt-norel']==1) {
       $relvideo = '?rel=0';
   }
   $embed = sprintf(
     '<iframe src="/%2$s%5$s" width="%3$spx" height="%4$spx" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe><p><a href="=%2$s" title="View video on YouTube">View video on YouTube</a></p>',
      get_template_directory_uri(),
      esc_attr($matches[1]),
      $defaultoptions['yt-content-width'],
      $defaultoptions['yt-content-height'],
      $relvideo
   );
   return apply_filters( 'embed_ytnocookie', $embed, $matches, $attr, $url, $rawattr );
 }

When you´re embed a youtube video in an iframe, you can enable the privacy-enhanced mode, so YouTube doesn´t store information about your web page visitors until they play the video.

I've tried to embed a video via oEmbed and the URL

http://www.youtube-nocookie/embed/xA3tfBTvH0c

but it didn't work. Is there a chance to implement a privacy-friendly solution with oEmbed?


EDIT I found this proposal and tried to customize it and it seems to work, but there is one thing that is not optimal. You can´t use the defined $content_width, because this solution needs a declaration of the height, too. Any ideas to this approach or do you have another?

 wp_embed_register_handler( 'ytnocookie', '#https?://www\.youtube\-nocookie\/embed/([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal', '#https?://www\.youtube\/watch\?v=([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal2', '#https?://www\.youtube\/watch\?feature=player_embedded&amp;v=([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );

 function wp_embed_handler_ytnocookie( $matches, $attr, $url, $rawattr ) {
   global $defaultoptions;
   $defaultoptions['yt-content-width'] = '680';
   $defaultoptions['yt-content-height'] = '510';
   $defaultoptions['yt-norel'] = 1;
   $relvideo = '';
   if ($defaultoptions['yt-norel']==1) {
       $relvideo = '?rel=0';
   }
   $embed = sprintf(
     '<iframe src="https://www.youtube-nocookie/embed/%2$s%5$s" width="%3$spx" height="%4$spx" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe><p><a href="https://www.youtube/watch?v=%2$s" title="View video on YouTube">View video on YouTube</a></p>',
      get_template_directory_uri(),
      esc_attr($matches[1]),
      $defaultoptions['yt-content-width'],
      $defaultoptions['yt-content-height'],
      $relvideo
   );
   return apply_filters( 'embed_ytnocookie', $embed, $matches, $attr, $url, $rawattr );
 }
Share Improve this question edited Jul 25, 2014 at 11:20 John asked Jul 25, 2014 at 9:10 JohnJohn 1111 silver badge8 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

At the moment WordPress only recognises youtube/watch, youtube/playlist and youtu.be. However there is wp_oembed_add_provider; try something like

wp_oembed_add_provider(
    '#http://(www\.)?youtube-nocookie\/embed.*#i',
    'http://www.youtube-nocookie/oembed', true );

(untested sorry). You could even overwrite the existing providers to redirect to -nocookie and then use the video shortcode as normal. And you can do this with add_filter('oembed_providers', ... ); too if you'd prefer.

发布评论

评论列表(0)

  1. 暂无评论