Last active
February 18, 2020 19:59
-
-
Save kellenmace/48d06e68cdf0fadbc894d5359afc593e to your computer and use it in GitHub Desktop.
Render Shortcodes in WPGraphQL Gutenberg Blocks
This file contains 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 | |
namespace Harness\Gutenberg; | |
use WPGraphQLGutenberg\WPGraphQLGutenberg; | |
use WPGraphQL\Registry\TypeRegistry; | |
use Harness\Interfaces\Hookable; | |
class ShortcodeBlockModifier implements Hookable { | |
public function register_hooks() { | |
add_action( 'graphql_register_types', [ $this, 'add_fields'] ); | |
} | |
public function add_fields() { | |
$type = WPGraphQLGutenberg::format_graphql_block_type_name( 'core/shortcode' ); | |
register_graphql_field( $type, 'renderedShortcode', [ | |
'type' => 'String', | |
'description' => __( 'Rendered shortcode markup.', 'harness' ), | |
'resolve' => function( array $block ) : string { | |
return do_shortcode( $block['originalContent'] ); | |
} | |
] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment