How to insert Gutenberg block inside post_content when using wp_insert_post()?
Asked Answered
S

2

6

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?

Supplejack answered 20/10, 2019 at 13:51 Comment(1)
You were pretty close with using the post_content as a "template". There are 2 convenient functions you can use. parse_blocks() which will convert the HTML-comments content to actual PHP arrays representing the blocks, and render_block() which will turn the arrays back into the content as it is saved to the DB. Here's a fine article about these functions billerickson.net/access-gutenberg-block-dataIndustrial
C
4

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 -->
Coleman answered 20/10, 2019 at 14:30 Comment(2)
It's more convenient than looking in the database, but it's still looks like a workaround. I wish there is an another solution, if i have to do this for every block i want to use to create any post it will be a bit tedious.Supplejack
I think some of the logic for the gutenberg might be generated by the javascript library it uses, can someone confirm this?Ballard
G
2

Today I had to update programmatically the content of Gutenberg blocks.

I used two methods:

  • parse_blocks() to read the existing array, which I could modify as any array in PHP.
  • render_blocks() to then add the content to the post post_content; the post array can then be saved via wp_update_post().

Reading this article was helpful https://www.billerickson.net/access-gutenberg-block-data/

My own project involved editing relationship acf custom fields to other custom post types within blocks but process should be fairly similar. Regarding OP's project: I would suggest to have a first post done manually to generate the template for an array (parse_block). Then customising this array should be relatively trivial.

Grafton answered 25/2, 2021 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.