简体   繁体   中英

How to get raw values in latest Wordpress Rest API v2?

When requesting posts/pages/media with Wordpress Rest API v2 I used to receive a 'raw' and a 'rendered' value for fields like title, guid and content. With the latest Wordpress version the 'raw' fields seems to have vanished. I need the raw data as this is stable over time. Plugins can add (changing) data to the rendered data.

Is there a (header) parameter I need to provide in the API call to get raw data?

Original data returned (fragment):

Array
(
    [id] => 1016
    [date] => 2017-11-08T16:18:29
    [date_gmt] => 2017-11-08T15:18:29
    [guid] => Array
        (
            [rendered] => https://example.com/wp-content/uploads/2017/03/image.jpg
            [raw] => https://example.com/wp-content/uploads/2017/03/image.jpg
        )

    [modified] => 2017-11-08T16:18:39
    [modified_gmt] => 2017-11-08T15:18:39
    [slug] => 888
    [status] => inherit
    [type] => attachment
    [link] => https://example.com/review/shopper/attachment/test/
    [title] => Array
        (
            [raw] => shopper image
            [rendered] => shopper image
        )

Latest WP API data:

Array
(
    [id] => 1016
    [date] => 2017-11-08T16:18:29
    [date_gmt] => 2017-11-08T15:18:29
    [guid] => Array
        (
            [rendered] => https://example.com/wp-content/uploads/2017/03/image.jpg
        )

    [modified] => 2017-11-08T16:18:39
    [modified_gmt] => 2017-11-08T15:18:39
    [slug] => 888
    [status] => inherit
    [type] => attachment
    [link] => https://example.com/review/shopper/attachment/test/
    [title] => Array
        (
            [rendered] => shopper image
        )

The API URL:

https://example.com/wp-json/wp/v2/media/32

Edit: I'm using oAuth1 for authentication ( https://wordpress.org/plugins/rest-api-oauth1/ ). Given that raw values are not returned unauthenticated, I'm starting to suspect that this plugin has issues sending the authentication during the information gathering.

You have to pass 'context' = 'edit' to get the raw content.

Or you can use this plugin: https://github.com/w1z2g3/wordpress-plugins/blob/master/post-raw-content.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM