Created
February 6, 2018 21:55
-
-
Save 2aces/f1f34f3fdb6e73800d227ded65c10dc2 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; | |
} |
here is a solution that works in wordpress 5
add_action('the_content','gutenprint',10,1);
function gutenprint($html) {
// check if we need to and can load the WP_Block_Parser
if ( !class_exists("WP_Block_Parser") && file_exists(ABSPATH . WPINC . '/class-wp-block-parser.php') ) {
include_once( ABSPATH . WPINC . '/class-wp-block-parser.php' );
}
if ( class_exists("WP_Block_Parser") && is_single() ) {
// do the actual parsing
$parser = new WP_Block_Parser;
$result = $parser->parse( $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
Thanks!
In my case I have had to add this to make it work:
include_once(WP_PLUGIN_DIR."/gutenberg/lib/parser.php");
Maybe is because i'm doing it outside functions.php
UPDATE: It works only on gutenberg plugin. Not over Wordpress 5.0...