Created
June 29, 2022 14:05
-
-
Save fabiankaegy/e2b5bd8b95fe5429b5e8552d14ae5be9 to your computer and use it in GitHub Desktop.
WordPress YouTube Embed block lite embed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'render_block', 'modify_youtube_embed_markup', 10, 2 ); | |
/** | |
* Modify block rendering to make YouTube embeds load a facade | |
* | |
* @param string|null $block_content The pre-rendered content. Default null. | |
* @param WPBlock $block The block being rendered. | |
* @return string modified Block HTML | |
*/ | |
function modify_youtube_embed_markup( $block_content, $block ) { | |
if ( is_admin() ) { | |
return $block_content; | |
} | |
if ( 'core/embed' === $block['blockName'] ) { | |
if ( 'youtube' === $block['attrs']['providerNameSlug'] ) { | |
return get_lite_youtube_embed_block_markup( $block ); | |
} | |
} | |
return $block_content; | |
} | |
/** | |
* get markup of lite youtube embed block | |
* | |
* @param WPBlock $block block class | |
* @return string markup | |
*/ | |
function get_lite_youtube_embed_block_markup( $block ) { | |
preg_match( '/<figcaption>(.*?)<\/figcaption>/s', $block['innerHTML'], $match ); | |
$caption_element = $match[0]; | |
parse_str( wp_parse_url( $block['attrs']['url'], PHP_URL_QUERY ), $youtube_query_params ); | |
$video_id = $youtube_query_params['v']; | |
ob_start(); | |
get_template_part( | |
'lite-youtube-embed', | |
'', | |
[ | |
'video_id' => $video_id, | |
'caption_element' => $caption_element, | |
] | |
); | |
$block_markup = ob_get_clean(); | |
return $block_markup; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Lite YouTube Embed | |
* | |
* @package namespace | |
*/ | |
$options = wp_parse_args( | |
$args, | |
[ | |
'video_id' => '', | |
'poster_image_url' => '', | |
'play_button_text' => __( 'Play Video', 'namespace' ), | |
'caption_element' => '', | |
] | |
); | |
$default_poster_url = sprintf( 'https://i.ytimg.com/vi/%s/hqdefault.jpg', $options['video_id'] ); | |
$poster_image_url = ! empty( $options['poster_image_url'] ) ? $options['poster_image_url'] : $default_poster_url; | |
?> | |
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"> | |
<lite-youtube class="lite-youtube-embed" videoid="<?php echo esc_attr( $options['video_id'] ); ?>" style="background-image: url(<?php echo esc_url( $poster_image_url ); ?>);"> | |
<div class="lite-youtube-embed__play-button__wrapper"> | |
<button type="button" class="lty-playbtn lite-youtube-embed__play-button"> | |
<span class="visually-hidden"><?php echo esc_html( $options['play_button_text'] ); ?></span> | |
</button> | |
<span class="lite-youtube-embed__play-button__text"><?php echo esc_html( $options['play_button_text'] ); ?></span> | |
</div> | |
</lite-youtube> | |
<?php | |
if ( isset( $options['caption_element'] ) ) { | |
echo wp_kses_post( $options['caption_element'] ); | |
} | |
?> | |
</figure> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment