Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active February 18, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/48d06e68cdf0fadbc894d5359afc593e to your computer and use it in GitHub Desktop.
Save kellenmace/48d06e68cdf0fadbc894d5359afc593e to your computer and use it in GitHub Desktop.
Render Shortcodes in WPGraphQL Gutenberg Blocks
<?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