Forked from kingkool68/parse_and_serialize_blocks.php
Created
December 14, 2023 02:57
-
-
Save alinademi/368cbc021466ff335b8eefed35974a53 to your computer and use it in GitHub Desktop.
A way to programmatically add a new block to a post using `parse_blocks()` and `serialize_blocks()` in WordPress. See https://www.facebook.com/groups/advancedwp/posts/7007114406017380/?__cft__[0]=AZWLn3Sa6BYQ9n2qmGTu1j2bPNBnUYdVBpYMEt8wfhaNSResEI_d-TICOX8GuVed1wDNlNq3Dxjh7iSxJnDNPVwgCvFwyGoM3HecqseR2cXeGhda3EfMslTHrfeQiFPwbG801Tl3scboksvZMDKsy0O…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function rh_filter_the_content( $the_content = '' ) { | |
$blocks = parse_blocks( $the_content ); | |
$new_block = array( | |
'blockName' => 'core/paragraph', | |
'attrs' => array(), | |
'innerBlocks' => array(), | |
'innerHTML' => '<p>Hello World</p>', | |
'innerContent' => array( | |
'<p>Hello World</p>', | |
), | |
); | |
$blocks[] = $new_block; | |
return serialize_blocks( $blocks ); | |
} | |
add_filter( 'the_content', 'rh_filter_the_content', 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment