Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcodes from Visual Composer #12

Open
web86 opened this issue Aug 12, 2021 · 1 comment
Open

Shortcodes from Visual Composer #12

web86 opened this issue Aug 12, 2021 · 1 comment

Comments

@web86
Copy link

web86 commented Aug 12, 2021

Hello! My posts are made with WpBakery (Visual Composer), so when I get them in WpGraphQL via wp-graphql-content-blocks I get a lot of not rendered shortcodes in output.

I solved this for content field, I registered a new field called "vsCodeShortcodesToHTML" and get there all decoded content. Then in my React component I just use this field instead of "content". To do so I added this code in my function,js:

// render js composer shortcodes to html in graphql response "addAllMappedShortcodes" field
add_action('graphql_register_types', function() {
	function vsCodeShortcodesToHTML( $post ) {
		if(class_exists('WPBMap') && method_exists('WPBMap', 'addAllMappedShortcodes')) {
			WPBMap::addAllMappedShortcodes(); 
		}
		return do_shortcode(get_post($post->ID)->post_content);
	}
	register_graphql_field('Post', 'vsComposerContentRendered', [
		'type' => 'String',
		'description' => __('the_content() VS Composer shortcodes rendered to HTML.', 'wp-graphql'),
		'resolve' => 'vsCodeShortcodesToHTML'
	]);
	register_graphql_field('Page', 'vsComposerContentRendered', [
		'type' => 'String',
		'description' => __('the_content() VS Composer shortcodes rendered to HTML.', 'wp-graphql'),
		'resolve' => 'vsCodeShortcodesToHTML'
	]);
	
});

It works for me untill I want to brake the content into blocks with your plugin. Shortcodes are still remaining.
Any thoughts?

@chriszarate
Copy link
Contributor

Per the README:

Shortcode/embed blocks are returned untransformed: the parsing of shortcodes is the responsibility of the front-end consuming the GraphQL endpoint. Only the name of the shortcode, its attributes and any nested content of the shortcode are returned in the GraphQL response.

This is mostly because its assumed your consuming application will want to render markup differently than your WordPress backend. So you are free to reimplement the shortcode in your consuming application, or you can filter graphql_blocks_output to transform the shortcode blocks and inject your desired HTML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants