Preview Changes with WordPress REST API (Authentication problem)
-
Hi there
I am using wordpress’s REST API to get the data.
For now the «preview Changes» does not have an effect at all, but just links to the old (frontend) page without the changes.What I would need is calling a different frontend template (Vue.js) which get’s the data directly from the REST API.
I found this post: https://wordpress.org/support/topic/preview-changes-in-rest-api/
And I understand that the preview is a revision of the page/post I am calling.
I also understand that I would expect the changed content by fetching a url like this:
https://DOMAIN.com/api/wp/v2/pages/51/revisions
But if I do that I get this response:{"code":"rest_cannot_read","message":"Sorry, you are not allowed to view revisions of this post.","data":{"status":401}}
This is due to the fact, that something with the authentication does not work properly. I thought I might find an answer here:
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/But there it says:
For developers using the built-in Javascript API, this is handled automatically for you. This is the recommended way to use the API for plugins and themes. Custom data models can extend wp.api.models.Base to ensure this is sent correctly for any custom requests.
Which I don’t fully understand, but «handled automatically for me» would let me think, that if I’m logged in in the backend, a cookie would let me see the restricted revisions content within the API. Obviously this is not the case.
And if the authentication problem is solved I am still unsure what to do next.
If I call the page with the «Preview Changes» button, I end up calling an url like
https://domain.com/pagename/?preview_id=51&preview_nonce=c83809a022&preview=trueDoes this mean I would have to rewrite the vue template to execute a different call to fetch the data, if the URLs GET params contain
preview=true
Hope someone can help me out here.
Thanks a lot and
cheersJ
— Edit:
I just tried passing the _wpnonce via GET paramater:
/api/wp/v2/pages/51/?_wpnonce=c83809a022
I edited the page within the backend and hit the preview button. This led me to my frontend (with the old content) but with an url
.../?preview_id=51&preview_nonce=c83809a022&preview=true
.
This is where I copied the nonce and added it to the url where I was calling the revisions directly.So the nonce should be quite new, but no I get this message:
{"code":"rest_cookie_invalid_nonce","message":"Cookie nonce is invalid","data":{"status":403}}
- The topic ‘Preview Changes with WordPress REST API (Authentication problem)’ is closed to new replies.