An open API service indexing awesome lists of open source software.

https://github.com/vuldin/raw-post-mod

Modifies WP REST-API post GET response to include a JSON version of the raw content
https://github.com/vuldin/raw-post-mod

plugin wordpress wp-api

Last synced: about 1 year ago
JSON representation

Modifies WP REST-API post GET response to include a JSON version of the raw content

Awesome Lists containing this project

README

          

# Raw post mod
Adds new fields `content_rawmod` and `excerpt_rawmod` to the post response from Wordpress API that contains a modified JSON version of the post's raw content.

## Install
This plugin will be available from the [Wordpress plugin directory](https://wordpress.org/plugins/) once it has gone through the approval process.
Instead you can follow the steps below:
```bash
git clone https://github.com/vuldin/raw-post-mod.git
zip -x *.git* -r raw-post-mod raw-post-mod/
```
Then upload this `raw-post-mod.zip` file from the plugins page of your wordpress admin portal.
Once uploaded, select `enable`.

## Purpose
While making a site using React/Next/Mobx and other javascript libraries, I wanted to get posts from wordpress and then render them as components in the app.
The issue is that Wordpress by default only returned a rendered version of the post content when making an unauthenticated request.
The raw content is a little closer to what is needed for being able to get the type of data I need, but it is only one step in the right direction.

Here is an example of what an unauthenticated post GET request returns by default:
```json
{
"id":19,
"date":"2017-06-22T10:03:07",
"date_gmt":"2017-06-22T17:03:07",
"guid":{
"rendered":"http:\/\/jlpwptest.localtunnel.me\/?p=19"
},
"modified":"2017-06-22T10:03:07",
"modified_gmt":"2017-06-22T17:03:07",
"slug":"title",
"status":"publish",
"type":"post",
"link":"http:\/\/jlpwptest.localtunnel.me\/2017\/06\/22\/title\/",
"title":{
"rendered":"Title"
},
"content":{
"rendered":"

First sentence of first paragraph. Second sentence of first paragraph.<\/p>\n

First sentence of second paragraph. Second sentence of second paragraph.<\/p>\n

\"\"<\/p>\n

First sentence of third paragraph. Second sentence of third paragraph.<\/p>\n",
"protected":false
},"excerpt":{
"rendered":"

First sentence of first paragraph. Second sentence of first paragraph. First sentence of second paragraph. Second sentence of second paragraph. First sentence of third paragraph. Second sentence of third paragraph.<\/p>\n",
"protected":false
},
"author":1,
"featured_media":0,
"comment_status":"open",
"ping_status":"open",
"sticky":false,
"template":"",
"format":"standard",
"meta":[],
"categories":[1],
"tags":[],
"_links":{
"self":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts\/19"}],
"collection":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts"}],
"about":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/types\/post"}],
"author":[{"embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/users\/1"}],
"replies":[{"embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/comments?post=19"}],
"version-history":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts\/19\/revisions"}],
"wp:attachment":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/media?parent=19"}],
"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/tags?post=19"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]
}
}
```
The `guid`, `title`, `content`, and `excerpt` only return a rendered version of that object's data.
For external webapps using the REST API this makes it difficult to determine for themselves how to best render this content.
This is especially the case with the `content` and `excerpt` objects.

## Feature
This mod reads the raw content and generates a JSON array.
Each entry in the array is the contents of the post's paragraph.

Here is an example of the same request above after this plugin is installed (see the `content_rawmod` and `excerpt_rawmod` properties):
```json
{
"id":19,
"date":"2017-06-22T10:03:07",
"date_gmt":"2017-06-22T17:03:07",
"guid":{
"rendered":"http:\/\/jlpwptest.localtunnel.me\/?p=19"
},
"modified":"2017-06-22T10:03:07",
"modified_gmt":"2017-06-22T17:03:07",
"slug":"title",
"status":"publish",
"type":"post",
"link":"http:\/\/jlpwptest.localtunnel.me\/2017\/06\/22\/title\/",
"title":{
"rendered":"Title"
},
"content":{
"rendered":"

First sentence of first paragraph. Second sentence of first paragraph.<\/p>\n

First sentence of second paragraph. Second sentence of second paragraph.<\/p>\n

\"\"<\/p>\n

First sentence of third paragraph. Second sentence of third paragraph.<\/p>\n",
"protected":false
},"excerpt":{
"rendered":"

First sentence of first paragraph. Second sentence of first paragraph. First sentence of second paragraph. Second sentence of second paragraph. First sentence of third paragraph. Second sentence of third paragraph.<\/p>\n",
"protected":false
},
"author":1,
"featured_media":0,
"comment_status":"open",
"ping_status":"open",
"sticky":false,
"template":"",
"format":"standard",
"meta":[],
"categories":[1],
"tags":[],
"content_rawmod":[
"First sentence of first paragraph. Second sentence of first paragraph.",
"First sentence of second paragraph. Second sentence of second paragraph.",
"[caption id=\"attachment_20\" align=\"alignnone\" width=\"300\"]\"alternative caption[\/caption]",
"First sentence of third paragraph. Second sentence of third paragraph."
],
"excerpt_rawmod":"First sentence of first paragraph. Second sentence of first paragraph. First sentence of second paragraph. Second sentence of second paragraph. First sentence of third paragraph. Second sentence of third paragraph.",
"_links":{
"self":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts\/19"}],
"collection":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts"}],
"about":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/types\/post"}],
"author":[{"embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/users\/1"}],
"replies":[{"embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/comments?post=19"}],
"version-history":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/posts\/19\/revisions"}],
"wp:attachment":[{"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/media?parent=19"}],
"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jlpwptest.localtunnel.me\/wp-json\/wp\/v2\/tags?post=19"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]
}
}
```

## Future plans
For the `excerpt_rawmod`, this mod must use the rendered property instead of raw since there seems to be no raw property even when authenticated for excerpt.
So for this transformation I strip the `

` tags from the string and return the response. This may need to be changed in the future.

The rawmod content is better than either the rendered or raw content, but it still has issues.
The main issue is the format that images and captions are in.
I'll be looking into how this plugin can modify this content so that it is in a more usable JSON format.
This could be solved by returning objects that gave details as to what type of block the content should be.

For example:
```json
{
"content_rawmod": [
{
"type": "text",
"value": "First sentence of first paragraph. Second sentence of first paragraph."
}
{
"type": "image",
"link": "http://jlpwptest.localtunnel.me/wp-content/uploads/2017/06/Test_card-300x169.png",
"caption": "caption text",
"alttext": "alternate text"
}
]
}
```