Plugins – Sending post data over REST API, how to parse shortcodes in post_content?

I’m using the REST API to publish posts outside of WordPress, and have run into a problem where all my shortcodes are not being processed. Specifically, most of my plugins’ shortcodes are getting processed but nothing is being converted to HTML from Visual Composer. I find that if I var_dump my REST API’s POST data function on the front end of the WordPress site (blank theme, theme is literally ‘var_dump this function’), the VC’s shortcodes get processed.

I’m not sure what I’m doing wrong here, but here’s my code for what the REST API returns:

function return_page_single( WP_REST_Request $request ) {
    $page_id = $request->get_param( 'page' ); 

    $args = array( 
        'id' => $page_id,
        'post_type' => 'page',
        'posts_per_page' => 1
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $page = $query->posts;
        $page_content = $page[0]->post_content;
        $page_content = apply_filters( 'the_content', $page_content );
        $page_content = do_shortcode( $page_content );
        $page[0]->post_content = $page_content;
        $query_return['page'] = $page;
        $query_return['query'] = $query->query;
    }
    wp_reset_postdata();
    return $query_return;
}

If I dump the content of my REST API result, I get shortcodes for VC but process everything else.

If I put the following in my subject:

var_dump( return_page_single( $request ) ); 

I get fully processed post content.

I don’t have a live example as this is being developed internally, but if it helps with troubleshooting I can probably set something up.

Leave a Comment