8

I'm using the WordPress REST API to get the HTML content of my WordPress page in an external application. I'm calling 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 in it's final HTML output and without the [vc_] shortcodes, eg: <p>Hello World</p>

The shortcodes are coming from the Visual Composer page builder plugin.

1
  • Having the same issue here. I've been trying to use a content filter to transform it to HTML. I posted on the support forum as well, so I hope to get a response either there or here. :) WP REST API support forum post Commented Apr 12, 2016 at 17:13

2 Answers 2

3

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

A question arose in a comment: how to get the custom CSS set for a page (post, etc.)? I modified the sample code in a way that it adds the custom CSS to the REST API response. You'll find the CSS in content/yb_wpb_post_custom_css.

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

Sign up to request clarification or add additional context in comments.

7 Comments

how do i get the CSS, rest api does not have CSS from Visual Composer ?
@GeorgeAlvis: I modified my answer so it now gives you a REST response with the custom CSS in it. WPB stores the custom CSS in a meta, not in the content, that's why you didn't get it with the request for content. meta can be added one-by-one, though - that's what I did.
$output['rendered'] = apply_filters( 'the_content', get_the_content()); thanks for the answer, i am retrieving page content from above line but now i want page CSS too in the api not the custom CSS. How do i Do it ? TIA
It's in the EDIT section of the answer - add a new field to content, or add a new field to the REST response.
I am not using custom CSS but expecting visual composer element CSS to be available in the API. @muka.gergely
|
1

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

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

Just in case if anyone was wondering.

2 Comments

How was this supposed to work for you? Did you add some filter? It's not clear.
I didn't add any filters, I was just getting the text from db and trying to get raw html without the shortcodes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.