Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert gutenberg block inside post_content when using wp_insert_post()?

I would like to generate a Gutenberg block in PHP.

I'm currently developing a WordPress plugin that import videos from YouTube and create a post for each video. I can insert the YouTube video inside the post_content but when i edit the post with the Gutenberg editor it doesn't display as a block.

I read most of the "Block Editor Handbook" here https://developer.wordpress.org/block-editor/ But i can't find anything except how to create custom block. I searched on google also, but everything I found was also about creating custom block. Yet I found that Gutenberg blocks are stored inside post_content as a html comment, but the comment seems to be generated with js via Gutenberg WYSIWYG editor.

I know that I could create a post with the blocks and copy the post_content from my database then use it as a "template" but I don't think it's a proper way.

Is there any documentation about using the blocks that come with WordPress (i.e.: embed, paragraph) and generate the html comment which is saved within post_content with PHP? Is it even possible?

like image 847
Olivier Avatar asked Oct 16 '22 10:10

Olivier


1 Answers

When you manually add a YouTube block, click the "Code Editor" view in the Tools & Options menu (right side). In the Code Editor view you will see the HTML needed for the editor to correctly parse the block.

For example:

<!-- wp:core-embed/youtube {"url":"https://www.youtube.com/watch?v=VIDEOID","type":"video","providerNameSlug":"youtube","className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://www.youtube.com/watch?v=VIDEOID
</div></figure>
<!-- /wp:core-embed/youtube -->
like image 167
Tim Jensen Avatar answered Oct 19 '22 01:10

Tim Jensen