I am creating some posts via the wp_insert_post()
function. When doing so the content gets saved in one big classic block. Is there anyway to force it to save as individual Paragraph blocks instead?
1 Answer
I ended up writing a function that wraps the <p>
tags in the block comments. This forced the content to be individual paragraph blocks in the editor. My content was only paragraphs but other block types would need to be implemented as well.
function blockify_content($content) {
//remove empty <p>
$pattern = "/<p[^>]*><\\/p[^>]*>/";
$content = preg_replace($pattern, '', $content);
//add block comments
$search = array('<p', '</p>');
$replace = array('<!-- wp:paragraph --><p', '</p><!-- /wp:paragraph -->');
$content = str_replace($search, $replace, $content);
return $content;
}
<p>
and</p>
paragraphs tags, without any attributes ?<p>
tags with classes to it.