YouTube oEmbed and privacy-enhanced mode

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.com/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\.com/embed/([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal', '#https?://www\.youtube\.com/watch\?v=([a-z0-9\-_]+)#i', 'wp_embed_handler_ytnocookie' );
 wp_embed_register_handler( 'ytnormal2', '#https?://www\.youtube\.com/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="https://www.youtube-nocookie.com/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.com/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 );
 }

1 Answer
1

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

wp_oembed_add_provider(
    '#http://(www\.)?youtube-nocookie\.com/embed.*#i',
    'http://www.youtube-nocookie.com/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.

Leave a Comment