Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress "REST API" - Render VisualComposer Content

Tags:

wordpress

I'm requesting Content from WordPress by the "REST API V2" Plugin. This works great. There is just one problem left: Contents created by "VisualComposer" Plugin are not rendered in de REST Response.

Response is:

[vc_row]Hello World . . .[/vc_row]

Response should be:

<div class="row">Hello World . . .</div>

How can this be achieved?? Thank you?

like image 441
mittererr Avatar asked Oct 30 '25 09:10

mittererr


2 Answers

I think you can find your answer here with WP REST API v2: https://github.com/WP-API/WP-API/issues/2578

The example below was taken from the link above (thank you, bradmsmith!)

Here is an example how to render the VC shortcodes on the content of a post:

add_action( 'rest_api_init', function ()
{
   register_rest_field(
          // if you need it to work with other (even custom post) types,
          // then you have to use an array:
          // array( 'page', 'post', 'custom_post_type', 'etc' )
          // this example only does the trick for 'page'
          // look at the link in the first EDIT section of this answer
          '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 );

   return $output;
}

EDIT

Here is the link for the register_rest_field() function: register_rest_field()

like image 158
muka.gergely Avatar answered Nov 03 '25 00:11

muka.gergely


I have solved it by using another REST Plugin for WordPress (JSON API). This plugin renders the response as expected. The VisualComposer shortcodes are now in HTML.

like image 20
mittererr Avatar answered Nov 02 '25 23:11

mittererr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!