• Resolved tamarazambrana

    (@tamarazambrana)


    I’m trying to change what the Youtube block displays. What I want to do is actually a bit more complex than this but I have at least tried to see if this code worked and nothing. Is it possible to customise the Youtube block?

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

    This work for the paragraph block:

    function ree_block_paragraph( $block_content, $block ) {
        if ( $block['blockName'] === 'core/paragraph' ) {
            $content = '<div class="wp-block-paragraph aaaa">';
            $content .= $block_content;
            $content .= '</div>';
            return $content;
        }
        return $block_content;
    }
    
    add_filter( 'render_block', 'ree_block_paragraph', 10, 2 );

    Thanks ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • I logged the contents of $block and found that the blockName has changed from core-embed/youtube to just core/embed. There’s a separate attribute that specifies a YouTube embed. So here’s updated code that does successfully add &rel=0.

    function ree_youtube_player($block_content, $block)
    {
    	if ("core/embed" === $block['blockName'] && "youtube" === $block['attrs']['providerNameSlug']) {
    		$block_content = str_replace('?feature=oembed', '?feature=oembed&rel=0', $block_content);
    	}
    	return $block_content;
    }
    add_filter('render_block', 'ree_youtube_player', 10, 2);

    In case it helps, here’s the console log statement I used to see the contents of $block.

    echo '<script>console.dir(' . json_encode($block) . ')</script>';

    Thread Starter tamarazambrana

    (@tamarazambrana)

    @rickymccallum87 thank you very much! it works!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Customise the Youtube block in Gutenberg’ is closed to new replies.