0

I have started posting YouTube videos as posts. I simply copy and paste the URL and WordPress figures out how to embed it. I like this workflow, but I don't like the embed code that WordPress generates. It seems that WordPress generates a code that allows suggested videos to appear at the end. I would like my embed code to not display the suggested videos at the end, just like as if I had generated it from the YouTube site. Is there a way to customize the generated embed code, or a plugin which provides this behavior?

screenshot of YouTube embed

  • You should add ?rel=0 in your video links. You can also create a shortcode for adding videos into your posts. That way you will be able to customize it as you want. – Robert hue Sep 24 '14 at 5:07
5

Something like this should do the trick and force rel=0 for all YouTube oembed results.

add_filter('oembed_dataparse','youtube_force_rel',10,3);
function youtube_force_rel($return, $data, $url) {
    if ($data->provider_name == 'YouTube') {
        return str_replace('feature=oembed', 'feature=oembed&rel=0', $return);
    } else {
        return $return;
    }
}
  • If this doesn't appear to be working, see the comment here: gist.github.com/jaygooby/1339360 - "oembed_dataparse runs before the content is cached, embed_oembed_html does it after it is rendered to the page" – Sarah Kemp Jun 29 '15 at 15:12
1

Here is the YouTube shortcode.

// youtube shortcode.
function sc_youtube_single( $att, $content = null ) {
    return '<iframe width="640" height="480" src="' . do_shortcode( $content ) . '?rel=0" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode( 'youtube', 'sc_youtube_single' );

And you can add videos in your content like this.

[youtube]http://www.youtube.com/embed/XQu8TTBmGhA[/youtube]

You don't need to add ?rel=0 now in shortcode. Just paste youtube video link. You can also modify this function's code to specify more variables like widths, height, autoplay etc.

EDIT

I understand the issue and for that I have another code. Now you can post youtube URLs from address bar too. What this function will do is get video ID from posted URL and change it into embed code automatically. So your users do not have to get the embed code.

// Get YouTube video ID from URL
function youtubeid( $url ) {
  $domain = parse_url( $url, PHP_URL_HOST );
  $url = esc_url( $url );
  if ( $domain == 'www.youtube.com' || $domain == 'youtube.com' ) {
    parse_str( parse_url( $url, PHP_URL_QUERY ) );
    $youtubeid = $v;
  } else {
    $youtubeid = '';
  }
  return $youtubeid;
}

// youtube shortcode.
function sc_youtube_single( $att, $content = null ) {
    return '<iframe width="640" height="480" src="http://www.youtube.com/embed/' . do_shortcode( youtubeid ( $content ) ) . '?rel=0" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode( 'youtube', 'sc_youtube_single' );
  • That might work. My only problem with this approach is that I have to wrap the URL in some shortcode tags and copy the embed URL instead of the "watch" URL. This will be too complicated for the people actually administering the WordPress blog. Is there any other way that would allow the user to copy and paste the URL they see in the browser's address bar? – Andrew Sep 24 '14 at 5:16
  • Please check edits. I have posted new code for this situation. – Robert hue Sep 24 '14 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.