Wordpress block editor embeds not working in theme (single.php)

I am trying to embed 'spotify' and 'youtube' services in the block editor. When added from the admin, they are displayed fine in the block editor, but when the post content is printed from the theme (single.php), only the respective urls are printed. The rendered HTML looks like:

For Spotify:

figure class=wp-block-embed is-type-rich is-provider-spotify wp-block-embed-spotify wp-embed-aspect-21-9 wp-has-aspect-ratiodiv class=wp-block-embed__wrapper
https://open.spotify.com/episode/xxxxxxamp;dl_branch=1
/div/figure

For Youtube:

figure class=wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratiodiv class=wp-block-embed__wrapper
https://www.youtube.com/watch?v=xxxxxx
/div/figure

In single.php, I'm obtaining the post content through get_posts() and printing the content with:

?php echo do_shortcode($tpostObj-content); ?

The other contents like texts, images etc are working fine. If the embeds are done with custom HTML iframe, they are working.

Thanks in advance.

Topic block-editor single embed get-posts youtube Wordpress

Category Web


That would be because they're using oEmbed, not a shortcode.

You should use the function the_content() to render the content of a post, as it applies all the things required to make embeds, shortcodes, etc work properly. If for some reason that's not an option, you can apply the content filter.

<?php echo apply_filters( 'the_content', $tpostObj->content ); ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.