2

I'm trying to get raw content through REST API

When I call "http://mysite.tld/wp-json/wp/v2/pages/456" I receive rendered content

"content": {
"rendered": "<div id=\"something\"></div>\n",
"protected": false
},

but I want the content to be unrendered

LE: I found something in /wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

if ( ! empty( $schema['properties']['content'] ) ) {
    $data['content'] = array(
        'raw' => $post->post_content,
        /** This filter is documented in wp-includes/post-template.php */
        'rendered'  => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
        'protected' => (bool) $post->post_password,
    );
}

If I replace

'rendered'  => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),

with

'rendered'  => $post->post_content,

it works well but how can I edit this from a custom plugin? btw: i noticed "raw" is missing from api response

2

This is expected, as it depends on the context.

The raw content will show up for the edit but not the view or embed context.

It's filtered through the WP_REST_Controller::filter_response_by_context() method.

You can always add a new field to the response with register_rest_field() where the get_callback callback receives an input array with post data, including the raw content.

But then you should watch out to not leak out e.g. password protected posts or content hidden with shortcodes.

  • W̶e̶l̶l̶,̶ ̶s̶o̶ ̶w̶h̶a̶t̶ ̶d̶a̶t̶a̶ ̶s̶h̶o̶u̶l̶d̶ ̶I̶ ̶s̶e̶n̶d̶ ̶t̶o̶ ̶A̶P̶I̶?̶ Thanks. I figured it out – Kovi Jun 16 '17 at 22:03
  • Hey! I know this post is very old but may you share the URL you used in order to get unrendered content in your JSON? – user3191334 Sep 3 '18 at 18:48
  • 1
    If we visit e.g. https://example.com/wp-json/wp/v2/posts then we only get rendered post content (with the the_content filter applied on the post_content field) in the view context, so we would need to add the raw database field (without applying the the_content filter) using register_rest_field() with a plugin ( I think there are examples in the Codex). But watch out for password protected content and shortcodes. Hope that helps @user3191334. – birgire Sep 4 '18 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.