-
-
Save Programmer095/40a3c011056ce3eaf79a4738c212f05a to your computer and use it in GitHub Desktop.
Frank Goossens (Futta) function to extract blocks from WP Gutenberg (Proof of Concept) as https://blog.futtta.be/2018/01/25/how-to-extract-blocks-from-gutenberg/
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
// proof-of-concept to extract all WordPress Gutenberg's blocks as array | |
// author: Frank Goossens (Futta) | |
// source: https://blog.futtta.be/2018/01/25/how-to-extract-blocks-from-gutenberg/ | |
add_action('the_content','gutenprint',10,1); | |
function gutenprint($html) { | |
// check if we need to and can load the Gutenberg PEG parser | |
if ( !class_exists("Gutenberg_PEG_Parser") && file_exists(WP_PLUGIN_DIR."/gutenberg/lib/load.php") ) { | |
include_once(WP_PLUGIN_DIR."/gutenberg/lib/load.php"); | |
} | |
if ( class_exists("Gutenberg_PEG_Parser") && is_single() ) { | |
// do the actual parsing | |
$parser = new Gutenberg_PEG_Parser; | |
$result = $parser->parse( _gutenberg_utf8_split( $html ) ); | |
// we need to see the HTML, not have it rendered, so applying htmlentities | |
array_walk_recursive($result, | |
function (&$result) { $result = htmlentities($result); } | |
); | |
// and dump the array to the screen | |
echo "<h1>Gutenprinter reads:</h1><pre>"; | |
var_dump($result); | |
echo "</pre>"; | |
} else { | |
echo "Not able to load Gutenberg parser, are you sure you have Gutenberg installed?"; | |
} | |
// remove filter to avoid double output | |
remove_filter('the_content','gutenprint'); | |
// and return the HTML | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment