Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 955
AFS Avatar asked May 30 '18 10:05

AFS


1 Answers

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

like image 163
zhigang Avatar answered Nov 15 '22 05:11

zhigang