Support » Fixing WordPress » Policing YouTube embed code using the oEmbed filter hook

  • Let’s say I want my WordPress functions to police YouTube embed code.

    The user wants protection from the YouTube feature in which, after screening a video clip, readers are offered “related videos.” Normally, a user can suppress “Related videos” by making sure the YouTube string includes the parameter “&rel=0.”

    I’d like to write a filter function that checks, via regular expressions, whether YouTube code on the site includes “&rel=0.” If it doesn’t, the function will guarantee it does, by appending it or replacing the unwanted “&rel=1” with the more desirable “&rel=0.”

    I’ve heard there is a WordPress hook called “oEmbed” that deals with this but I can’t find a good example of how to use it. Could someone explain how it would work? Any help much appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m looking for this same functionality – did you ever find a solution?

    I love the ability for users to add video simply by pasting the YouTube url. No need to deal with youtube embed code and options – just paste and go. And it looks like WP generates it’s own iframe code, utilizing the standard youtube iframe options. I’d like to hook in to this process and simply add that ‘”rel”=0’.

    Any thoughts?

    Thanks for the link but that wasn’t my question. I am – or was – looking for a way to inject one of those player parameters in to the WP code that was manipulating a youtube url in to an iframe chunk of code.

    Found my solution:

    function remove_related_videos($embed) {
    	if (strstr($embed,'')) {
    		return str_replace('?fs=1','?fs=1&rel=0',$embed);
    	} else {
    		return $embed;
    add_filter('oembed_result', 'remove_related_videos', 1, true);

    Oh, I was trying to point you to the HTML5 section of the page and some additional features available such as explained in the video at the bottom…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Policing YouTube embed code using the oEmbed filter hook’ is closed to new replies.