Skip to content

Commit 2c11541

Browse files
committed
Block Creation Example
- Create a block programmatically and serialize it. - copied from https://developer.wordpress.org/reference/functions/serialize_block/
1 parent 6d10f6b commit 2c11541

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

quick-weblog.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,29 @@ function quick_weblog_submit_form() {
148148
'tags_input' => $tags,
149149
'post_status' => 'publish'
150150
);
151+
152+
/*
153+
* Create a block programmatically and serialize it.
154+
* https://developer.wordpress.org/reference/functions/serialize_block/
155+
*/
156+
$block_name = 'core/paragraph';
157+
$innerHTML = 'Sample paragraph text.';
158+
159+
$converted_block = new WP_Block_Parser_Block( $block_name, array(), array(), $innerHTML, array( $innerHTML ) );
160+
// WP_CLI::log( print_r( $converted_block, true ) );
161+
162+
$serialized_block = serialize_block( (array) $converted_block );
163+
// WP_CLI::log( $serialized_block );
164+
165+
// Create a new post
166+
$post_data = array(
167+
'post_title' => $title,
168+
'post_content' => $serialized_block,
169+
'post_category' => array( $category ),
170+
'tags_input' => $tags,
171+
'post_status' => 'publish'
172+
);
173+
151174
$post_id = wp_insert_post( $post_data );
152175

153176
// Redirect to the new post

0 commit comments

Comments
 (0)