Created
February 6, 2018 21:55
-
-
Save 2aces/f1f34f3fdb6e73800d227ded65c10dc2 to your computer and use it in GitHub Desktop.
Revisions
-
2aces created this gist
Feb 6, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // 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; }