Created
May 28, 2019 00:50
-
-
Save mlipscombe/2e6d82258c584d4a1d6ba4265250afbc to your computer and use it in GitHub Desktop.
Render Elementor-styled content in WP-GraphQL
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
add_action('graphql_register_types', function() { | |
$post_types = WPGraphQL::$allowed_post_types; | |
if (!empty($post_types) && is_array($post_types)) { | |
foreach($post_types as $post_type) { | |
$post_type_object = get_post_type_object($post_type); | |
register_graphql_field($post_type_object->graphql_single_name, 'styledContent', [ | |
'type' => 'String', | |
'description' => __('The styled elementor content of the page', 'cottontailpress-headless'), | |
'resolve' => function($post) { | |
$content = ''; | |
if (class_exists('\Elementor\Plugin')) { | |
error_log('class_exists'); | |
$plugin = \Elementor\Plugin::instance(); | |
$content = $plugin->frontend->get_builder_content($post->ID, true); | |
} | |
return $content; | |
} | |
]); | |
} | |
} | |
}); |
Even better, replace it with WPGraphQL::get_allowed_post_types( 'objects' );
add_action( 'graphql_register_types', function() {
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );
foreach( $post_types as $post_type_object ) {
register_graphql_field(
$post_type_object->graphql_single_name,
'styledContent',
[
'type' => 'String',
'description' => __('The styled elementor content of the page', 'my-translation-domain'),
'resolve' => function( $post ) {
if ( class_exists('\Elementor\Plugin') ) {
return null;
}
$plugin = \Elementor\Plugin::instance();
return $plugin->frontend->get_builder_content($post->ID, true) ?: null;
},
]
);
}
} );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
replace
$post_types = WPGraphQL::$allowed_post_types; to $post_types = WPGraphQL::get_allowed_post_types();