ardend
Forum Replies Created
-
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] Creating post with category doesn't workOk got this sorted out. For anyone else using Angular, apparently Angular’s HTTP post serializes data in a way that php won’t normally parse correctly, so you have to modify the headers in Angular. I found this on stackoverflow:
Check out the answer from Thomas Graziani, about 4 or so down the page. Besides the transform part added to the app.js file, changing the data part of the request code from “params” to “data” made it work. Hope this helps!
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] Creating post with category doesn't workActually in the first line of code i set the value to an array like you mentioned for the second example, only when its actually sent it splits it up like you see (32?categories=3&categories=8). Is it the headers maybe?
If it helps, formData is initialized as an empty json to start:
$scope.formData = {};
I feel like there is some specific combination of how to format the param and how to pass it that I am missing.
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] Creating post with category doesn't workHi Daniel,
Here is the code I am using to send the request, which looks right but the param for categories seem to get messed up when sent as you’ll see below:
$scope.formData.categories = [3, 8]; $http({ method: 'POST', url: ($scope.api + 'wp/v2/posts/32'), params: $scope.formData, headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': RestAPI.nonce } }) .success(function (result) { console.log('Success!'); }).error(function () { console.log('Fail!'); });And here is what happens with the request when i pass it like that:
http://sitename/wp-json/wp/v2/posts/32?categories=3&categories=8This simply removes any categories in wordpress associated with the post.
When i try and pass it like this: $scope.formData.categories = “[3, 8]”;
The request looks like this:
http://sitename/wp-json/wp/v2/posts/32?categories=%5B3,+8%5DThe request looks right, but all this does is set the post to uncategorized. Any help would be great thanks!
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] Creating post with category doesn't workI replied to another recent post about this same issue. It seems that creating or editing Tag and Categories isn’t working properly. Every format of passing the arguments seems always result in the same error in the response:
<b>Warning</b>: array_map(): Argument #2 should be an array in <b>/sitename/wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-posts-controller.php</b> on line <b>918</b><br />Hoping for a fix or any info soon as creating a post without a category is going to be an issue for most I would assume. I know the team is slammed I’m sure working towards their integration into WP, but hopefully we can find out how to fix or get around this.
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] How to create a post with category?Just bumping this as I still can’t get any kind of category request to work. Any ideas? I know the team is swamped, so any help or update is appreciated, thanks!
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] How to create a post with category?I cant get this to work either. Here is the javascript post I am trying to use to edit the categories of a post:
$http({ method: 'POST', url: ($scope.api + 'wp/v2/posts/32'), params: JSON.stringify({'categories': [3,8,11]}), headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': RestAPI.nonce } }) .success(function (result) { console.log('Success!'); }).error(function () { console.log('Fail!'); });But this does nothing at all. The post sends successfully but there is no change in the categories for the post. Any ideas on how to modify the params above to the way the API is looking for?
Thanks for any help!
Forum: Plugins
In reply to: [WordPress REST API (Version 2)] WP API can't create a postI am having the same issue you originally had Goharika. I am using angular in the front end and I want to use basic authorization. I have enqueued the api and created the nonce:
wp_localize_script( 'angular-core', 'RestAPI', array( 'url' => get_bloginfo('wpurl').'/wp-json/wp/v2/', 'nonce' => wp_create_nonce( 'wp_rest' ) ) );I then have this form in html:
<form ng-submit="processForm()"> <!-- Title --> <div id="title_field" class=""> <label>Title</label> <input type="text" name="title" class="" placeholder="Name of item" ng-model="formData.title"> </div> <!-- Submit Button --> <button type="submit" class=""> <span class=""></span> Submit! </button> </form>And this is the processForm function in my controller:
$scope.processForm = function() { console.log(RestAPI.nonce); $http({ method: 'POST', url: '/wp-json/wp/v2/posts', params: $scope.formData, transformRequest: angular.identity, headers: { 'Content-Type': 'multipart/form-data', 'X-WP-Nonce': RestAPI.nonce } }) .success(function (result) { console.log('Success!'); }).error(function () { console.log('Fail!'); }); };Adding the headers portion of the submit function got me from a 401 error to a 403 error, saying the nonce is invalid. I don’t know what the deal is.
If it matters, while logged into the admin panel, if i go to the site and try and access users/me (which i know has issues listed in the documentation) I simply get logged out and repeatedly asked to login. Maybe i am misunderstanding the nonce usage?