Creation of blocks from PHP
-
Hello folks,
I’m recreating old site with wordpress. The old site has many pages generated from structured content, the pages consist of a mixture of headings, paragraphs and images in various order. I want to create one page in wordpress and generate the content through plugin. In this plugin I would like to generate standard core blocks, they should be correct rendered on frontend through theme, but there is no need to edit them or store in database. So the question is about the recommended way to insert blocks on fly to the page. My idea was to do it in the filter ‘the_content’ and output the blocks through serialize_block() as described in the comment to this function on in the reference doc, something like:
... $block_name = 'core/paragraph'; $innerHTML = 'Sample paragraph text.'; $converted_block = new WP_Block_Parser_Block( $block_name, array(), array(), $innerHTML, array( $innerHTML ) ); $serialized_block = serialize_block( (array) $converted_block ); echo $serialized_block; ...
My questions, is it the recomended way to create blocks on the fly?
Will the blocks have the needed css classes?
Will they be correctly rendered and displayed through themes?Regards,
Annie
- The topic ‘Creation of blocks from PHP’ is closed to new replies.