WordPress.org

Ready to get started?Download WordPress

Forums

Help needed: Adding YouTube parameters to oEmbed function (6 posts)

  1. rikay
    Member
    Posted 8 months ago #

    Hi there,

    I want to add these youtube parameters ?rel=0&?wmode=transparent&iv_load_policy=3&showinfo=0&vq=hd1080 to the youtube iframe that is created with the oembed function. For Example: pasting a youtube link (http://www.youtube.com/watch?v=-McFIAlP0Ow) inside a post will now only result in this:

    <iframe width="100%" height="394" src="http://www.youtube.com/embed/-McFIAlP0Ow?feature=oembed" frameborder="0" allowfullscreen="" style="height: 420px;"></iframe>

    I want the oembed function to automatically add the earlier mentioned parameters, so the iframe will look like this:

    <iframe width="100%" height="394" src="http://www.youtube.com/embed/-McFIAlP0Ow?feature=oembed?rel=0&?wmode=transparent&iv_load_policy=3&showinfo=0&vq=hd1080" frameborder="0" allowfullscreen="" style="height: 420px;"></iframe>

    I know i can add these parameters manually by adding them to the iframe html code provided by youtube. But i have a lot of bloggers and want to make it as simple as possible for them to create youtube posts. So rather then let them use the youtube iframe code i want them to just paste the youtube link inside the posts and let oembed do all the work for them.

    I already know the oembed functions are located in wp-includes/class-oembed.php, but my lack of javascript and php skills stops me from randomly adding coding to this file.

    I really hope someone can provide me with the right piece of php coding or javascript!

    Thanks and Merry Christmas to all of you!

  2. bcworkz
    Member
    Posted 8 months ago #

    Hook the 'oembed_result' filter and use PHP string functions to insert the required parameters in the passed iframe HTML, then return the result.

  3. r1kay
    Member
    Posted 8 months ago #

    Thanks for the feedback, but I really lack PHP skills. Could you provide me with the right code?

  4. bcworkz
    Member
    Posted 8 months ago #

    The basic code frame work looks like the following. It's untested, the string replacement details may need to be tweaked to get the correct results. This would go on your theme's functions.php file.

    add_filter('oembed_result', 'r1k_fix_oembed')
    function r1k_fix_oembed( $embed ) {
       return str_replace('?feature=oembed', '?feature=oembed&rel=0&?wmode=transparent&iv_load_policy=3&showinfo=0&vq=hd1080', $embed );
    }
  5. r1kay
    Member
    Posted 8 months ago #

    Thanks bcworkz,

    I've added to code to the theme's functions.php but it will break the website. Do you have any suggestions what part of the code needs to be tweaked?

  6. r1kay
    Member
    Posted 8 months ago #

    bcworkz, i've managed to modify a piece of coding that was posted by someone else in a similar thread 2 years ago.

    to make it work i used this:

    function Oembed_youtube_no_title($html,$url,$args){
        $url_string = parse_url($url, PHP_URL_QUERY);
        parse_str($url_string, $id);
        if (isset($id['v'])) {
            return '<iframe width="100%" height="394" src="http://www.youtube.com/embed/'.$id['v'].'?rel=0&?wmode=transparent&iv_load_policy=3&showinfo=0&vq=hd1080" frameborder="0" allowfullscreen></iframe>';
        }
        return $html;
    }
    add_filter('oembed_result','Oembed_youtube_no_title',10,3);

Reply

You must log in to post.

About this Topic