Snippets

Joseph Dickson Parse block - YouTube

Created by Joseph Dickson
<?php
//  Add to functions.php
 
function display_post_youtube_block() {
 
    global $post;
     
    $blocks = parse_blocks( $post->post_content );
     
        foreach( $blocks as $block ) {
 
            // YouTube's block name
            if( 'core-embed/youtube' === $block['blockName'] ) {
             
            echo apply_filters( 'the_content', render_block( $block ) );
             
            break;
             
        }
         
    }
     
}
 
?>
<?php
// Add within the loop
 
// Check if the YouTube block is used in the post using the blockName
if ( has_block( 'core-embed/youtube' ) ) {
  
    // Display the YouTube block from the post
    display_post_youtube_block();
  
}
 
?>

Comments (1)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.