I have a wordpress page from which I am trying to create a post using the WP AJAX REST API. My expectation is that if the user is already logged in the auth cookie will be in place and I'll be able to do the AJAX POST with the user's context. Yet I'm getting an error that the user is not authorized. What am I missing? Thanks
Asked
Active
Viewed 447 times
0
-
Are you using wordpress.com or a plugin on your own site? if wp.com https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/new/ – David May 10 '15 at 00:13
-
self hosted wordpress, JSON API plugin (https://wordpress.org/plugins/json-api/) – Z . May 10 '15 at 02:11
-
im not too familiar with that plugin but the documentation states that a nonce is required. It prob dosen't check for logged in status as the rest plugins are usually not designed for ajax calls from within the site (there is a core system for ajax) https://wordpress.org/plugins/json-api/other_notes/#Method:-create_post – David May 10 '15 at 14:20
-
what does this mean? how should I call `wp_nonce_field`? nonce - available from the get_nonce method (call with vars controller=posts and method=create_post) I'm doing `wp_nonce_field('posts', 'create_post')` – Z . May 10 '15 at 20:20
-
added a answer below. I was wrong earlier this plugin does check if the user is logged in as well (needs to have edit_posts permission) – David May 10 '15 at 21:51
1 Answers
0
This plugin is not kept up to date but adding a new post should work as follows:
Go to the settings -> json api
and activate posts controllerget nonce
http://www.example.com/?json=core.get_nonce&controller=posts&method=create_post&callback=?
Grab the nonce from the reponse, Response should be:
?({"status":"ok","controller":"posts","method":"create_post","nonce":"38nonce84"})
4.Post a post
www.example.com/?json=posts.create_post&nonce=383nonce84&title=newpost&content=blahblabh&status=publish

David
- 5,897
- 3
- 24
- 43