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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0