Skip to content

Instantly share code, notes, and snippets.

@2aces
Created February 6, 2018 21:55
Show Gist options
  • Select an option

  • Save 2aces/f1f34f3fdb6e73800d227ded65c10dc2 to your computer and use it in GitHub Desktop.

Select an option

Save 2aces/f1f34f3fdb6e73800d227ded65c10dc2 to your computer and use it in GitHub Desktop.

Revisions

  1. 2aces created this gist Feb 6, 2018.
    35 changes: 35 additions & 0 deletions futta-gutenprint.php
    Original 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;
    }