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

1 Answer
1

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *