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

function bolster_oembed_filter( $return, $data ) {
            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);
            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.


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.

| improve this answer | |
  • If and when this breaks again look at default-filters.php in core to understand how WP sets the majority of actions and filters, and at what priority. – Josh Habdas Jun 19 '17 at 14:18

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.