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/embed
and youtube
is 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.
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/embed
and youtube
is 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.
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.
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
The variation YouTube has a providerNameSlug
attribute that is provided during the registerBlockVariation()
call with the value of youtube
. This attribute is available in the block filter's 2nd argument under the atts
/providerNameSlug
key. Based on that your filter should look like:
function wpftw_modest_youtube_player( $block_content, $block ) {
if( "core/embed" === $block['blockName'] && ($block['attrs']['providerNameSlug']??'') === 'youtube' ) {
// do your things...
}
return $block_content;
}
add_filter( 'render_block', 'wpftw_modest_youtube_player', 10, 3);
But it is rather pointless to filter all blocks, it just reduces performance and burns CPU cycles in vain, so a better approach is:
function wpftw_modest_youtube_player( $block_content, $block ) {
if( ($block['attrs']['providerNameSlug']??'') === 'youtube' ) {
// do your things...
}
return $block_content;
}
add_filter( 'render_block_core/embed', 'wpftw_modest_youtube_player', 10, 3);
This filter is only applied to embed blocks.
core/embed
and you need to check the other attributes for the active variation. Doesvar_dump($block);
help you identify where it is saved? – kero Commented Jan 26, 2021 at 13:19youtube
- but how do I test for the variation's attribute? does something like$block['blockVariation']
make any sense? – 1NN Commented Jan 26, 2021 at 13:28$block
. Does$block['blockVariation']
exist? – kero Commented Jan 26, 2021 at 14:33$block['blockVariation']
does NOT exist .. I don't really have the php basics so I'm a bit at loss.. – 1NN Commented Jan 26, 2021 at 14:43