Get raw HTML output from the WordPress REST API

I use the WordPress REST API to get the HTML content of my WordPress page in an external application. I call this mysite / wp-json / wp / v2 / pages / 10 and it returns:

"content": { "rendered": "[vc_column_text]Hello World[/vc_column_text]" } 

Is there any way to return the code to the final HTML code without shortcodes [vc_], for example: <p>Hello World</p>

The shortcodes are from the Visual Composer page builder plugin .

+6
source share
2 answers

Found and answer here: https://github.com/CompassHB/web/issues/67#issuecomment-245857301

The example below is taken from the link above:

 /** * Modify REST API content for pages to force * shortcodes to render since Visual Composer does not * do this */ add_action( 'rest_api_init', function () { register_rest_field( 'page', 'content', array( 'get_callback' => 'compasshb_do_shortcodes', 'update_callback' => null, 'schema' => null, ) ); }); function compasshb_do_shortcodes( $object, $field_name, $request ) { WPBMap::addAllMappedShortcodes(); // This does all the work global $post; $post = get_post ($object['id']); $output['rendered'] = apply_filters( 'the_content', $post->post_content ); // EDIT: add custom CSS to $output: $output[ 'yb_wpb_post_custom_css' ] = get_post_meta( $object[ 'id' ], '_wpb_post_custom_css', true); return $output; } 

EDIT

In the comment, the question arose: how to get a custom CSS set for a page (publications, etc.)? I modified the sample code so that it adds custom CSS to the REST API Response . You will find CSS in content/yb_wpb_post_custom_css .

Another way is to add another field to the REST API response containing this CSS. The key is that custom CSS is set for page / post / etc. has a meta key _wpb_post_custom_css .

+1
source

About 2 years late for the party, the following worked for me:

 $output['rendered'] = apply_filters( 'the_content', get_the_content() ); 

Just in case, if anyone is interested.

+1
source

Source: https://habr.com/ru/post/1243132/


All Articles