Manipulating oembed_dataparse stopped working for YouTube embeds

My go-to code to manipulate YouTube embeds does not work anymore. Can anyone explain to me what I am doing wrong?

This is my code in functions.php

// OEMBED FILTER
function bolster_oembed_filter( $return, $data ) {
    // YOUTUBE (ADD PARAMS, NOCOOKIE DOMAIN)
    if(
            is_object( $data ) 
        &&  property_exists( $data, 'provider_name' )
        &&  'YouTube' === $data->provider_name
    ){
        $return = preg_replace("@src=(['\"])?([^'\">\s]*)@", "src=$1$2?rel=0&showinfo=0&wmode=opaque", $return);
        $return = preg_replace('/youtube\.com\/(v|embed)\//s', 'youtube-nocookie.com/$1/', $return);
    }
    // VIDEO (ADD WRAPPER)
    if(
            is_object( $data ) 
        &&  property_exists( $data, 'type' )
        &&  'video' === $data->type
    ){
        $return = '<figure class="media">'.$return.'</figure>';
    }
    return $return;
}
add_filter('oembed_dataparse', 'bolster_oembed_filter', 10, 2 );

As you can see I check wether the provider is YouTube and manipulate the output when that returns true. I also wrap the output in a <figure> tag when the embed type is video.

When I embed a Vimeo video the embed code gets wrapped in the <figure> tag. When I embed a YouTube video it gets embedded, but none of my manipulations are applied. I know this used to work, it seems a recent version of WordPress has changed something that causes YouTube oembed to not be affected by oembed_dataparse anymore.

I did clean the oembed cache before testing, so I am sure that is not the reason this happens.

1 Answer
1

I think I solved by doing this:

add_filter('oembed_dataparse', 'bolster_oembed_filter', 0, 2 );

I changed the priority of the add_filter statement to 0, now it works again.

Leave a Comment