2

I'm using a code snippet in functions.php to modify all youtube embeds, in order to add extra player parameters. Prior to WordPress 5.6, the youtube blocks could be filtered with if( "core-embed/youtube" === $block['blockName']) { // do something }

However, from WP 5.6 on the block name now is core/embedand youtubeis a Variation.

I have tried if( "core/embed/youtube" === $block['blockName']) as well as if($block['blockName'] == 'core/embed/youtube'), but this doesn't work.

My full original code (adapted from this article) is:

function wpftw_modest_youtube_player( $block_content, $block ) {
    if( "core-embed/youtube" === $block['blockName'] ) {
        $block_content = str_replace( '?feature=oembed', '?feature=oembed&modestbranding=1&showinfo=0&rel=0&cc_load_policy=1', $block_content );
    }
    return $block_content;
}
add_filter( 'render_block', 'wpftw_modest_youtube_player', 10, 3);

I'm relatively new to Wordpress and to php, so any clarity is appreciated. I've tried to google an answer and consulted the Block Editor Handbook, but can't figure out the answer.

4
  • I can't test this right now, so just commenting. The name should be core/embed and you need to check the other attributes for the active variation. Does var_dump($block); help you identify where it is saved?
    – kero
    Jan 26, 2021 at 13:19
  • @kero I know the Variation's name is youtube - but how do I test for the variation's attribute? does something like $block['blockVariation'] make any sense?
    – 1NN
    Jan 26, 2021 at 13:28
  • That's why I meant by inspecting what values are actually in $block. Does $block['blockVariation'] exist?
    – kero
    Jan 26, 2021 at 14:33
  • I don't have a debugging environment, but it seems $block['blockVariation']does NOT exist .. I don't really have the php basics so I'm a bit at loss..
    – 1NN
    Jan 26, 2021 at 14:43

1 Answer 1

3

This whole block variations thing is a complete red herring, a wild goose chase!

But lets just follow it to its conclusion before I share the actual solution, which is much simpler than you'd think.


You wouldn't look at the variation itself, variations set different attributes, and it's the attributes you should look for, not the variation.

For example, here's is the twitter variation as defined in packages/block-library/src/embed/variations.js:

    {
        name: 'twitter',
        title: 'Twitter',
        icon: embedTwitterIcon,
        keywords: [ 'tweet', __( 'social' ) ],
        description: __( 'Embed a tweet.' ),
        patterns: [ /^https?:\/\/(www\.)?twitter\.com\/.+/i ],
        attributes: { providerNameSlug: 'twitter', responsive: true },
    },

Instead of looking at the block variation, check the providerNameSlug attribute instead.

But This is All Barking Up The Wrong Tree

This is not how you would do this at all, in fact your problem is completely unrelated to blocks and the editor!

If you want to modify OEmbed URLs, don't modify the blocks that render OEmbeds, modify the OEmbed! WordPress has had filters for this for years, long before the block editor was introduced.

Thankfully, someone asked how to do this in 2011, and the answer should still work:

https://wordpress.stackexchange.com/a/14438/736

4
  • OK. I get the sense of your answer, also because in the meantime I've found a solution targeting the OEmbed to change the youtube embed parameters. If anyone else should be looking for it it is on this website
    – 1NN
    Jan 27, 2021 at 9:19
  • Just for the sake of learning something: would the following work in order to target at block level ? if( 'youtube' == $block['providerNameSlug']) { // do something }
    – 1NN
    Jan 27, 2021 at 9:22
  • Both yes and no, the canonical answer to that is it depends on what you're doing. You should not be inspecting the block attributes to change OEmbed markup. It also is not a generic method of targeting block variants. Remember, a blocks variants are just predefined sets of attributes with an icon/title attached, no matter which variant is used, it's still an embed block. What you needed to do was detect if the block is embedding youtube, not if it was a youtube variant. Case in point, you could have added a raw embed block with no variants and inserted a youtube video URL.
    – Tom J Nowell
    Jan 27, 2021 at 15:58
  • Also, this is one of those situations where asking that question is incredibly inefficient and slow. var_dump or json_encode it and directly see for yourself
    – Tom J Nowell
    Jan 27, 2021 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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