Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posts endpoint example code PHP/jQuery #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions examples/posts/jQuery-AJAX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Requests which require authentication assume that you have enqueued the client-js and therefore the following variables are localized:
WP_API_Settings.root
WP_API_Settings.nonce


### Get Recent Posts
```

$.ajax({
type: 'GET',
url: WP_API_Settings.root + '/posts,
dataType: 'json',
success: function(posts){
$.each( posts, function(index, post) {
//do something with each post
});
},
error: function(error){
console.log(error);
}
});

```

### Get A Specific Post
```
$.ajax({
type: 'GET',
url: WP_API_Settings.root + '/posts/42,
dataType: 'json',
success: function(post){
//do something with posts
},
error: function(error){
console.log(error);
}
});

```

### Create A Post

```
//create post data
var post = array(
'title' : 'Lord of the Rings',
'post_type' : 'book',
'content_raw' : 'Best book ever.'
);

//prepare data
var data = JSON.stringify( post );


$.ajax({
type:"POST",
url: WP_API_Settings.root + '/posts,
dataType : 'json',
data: data,
beforeSend : function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', JP_POST_EDITOR.nonce );
},
success: function(response) {
alert( 'Post created. ID is ' + response.ID );
},
failure: function( response ) {
alert( 'FAIL!' );
}
});
```

### Edit A Post

```
//post data to edit
var post = array(
'title' : 'The Lord Of The Rings, Being the third part of the Lord of the Rings'
);

//prepare data
var data = JSON.stringify( post );

$.ajax({
type:"POST",
url: WP_API_Settings.root + '/posts/42,
dataType : 'json',
data: data,
beforeSend : function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', JP_POST_EDITOR.nonce );
},
success: function(response) {
alert( 'Post ' + response.ID ' + ' updated succesfully.' );
},
failure: function( response ) {
alert( 'FAIL!' );
}
});
```
164 changes: 164 additions & 0 deletions examples/posts/php.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
Requests to the API, from within WordPress, or any other HTTP request should use [the WordPress HTTP API](http://codex.wordpress.org/HTTP_API).

All URLs should be constructed using either the function `json_url()` or `get_json_url()` when using multisite. These functions will return the root URL for the API, according to the current permalink structure. In addition, they will take into account the current value of the 'json_url' filter, which can be used to change the root url for the API.

The URL for the simplest request to the posts route, which would return the most recent posts, would be constructed with `json_url( 'posts' );`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives you the URL for the local installation, but if you're running this locally, it's simpler just to run the code internally. Presumably you'll want external URLs instead.


### Get Most Recent Posts
* Number of posts will equal the posts_per_page option
``` php
//create url
$url = json_url( 'posts' );

//Make request via the WordPress HTTP API
$response = wp_remote_get( $url );


//Check for error
if ( ! is_wp_error( $response ) ) {
//get just the body
$data = wp_remote_retrieve_body( $response );

//decode
$data = json_decode( $data );
}


}
```

### Get A Post By ID
* In this case post ID 42 will be retrieved.

``` php
//create url
$url = json_url( 'posts/42' );

//Make request via the WordPress HTTP API
$response = wp_remote_get( $url );


//Check for error
if ( ! is_wp_error( $response ) ) {
//get just the body
$data = wp_remote_retrieve_body( $response );

//decode
$data = json_decode( $data );
}


}
```

### Use filters
In order to add filters to the request URL, use `add_query_args()` to generate the query string. In this 8 posts, in descending order, whose author has the username "fry" are returned:
``` php

//build array of args
$args = array(
'filter[author_name]' => 'fry',
'filter[posts_per_page]' => 8,
'filter[order]' => 'DESC'
);

$url = add_query_arg( $args, json_url( 'posts' ) );

//Make request via the WordPress HTTP API
$response = wp_remote_get( $url );

//Check for error
if ( ! is_wp_error( $response ) ) {
//get just the body
$data = wp_remote_retrieve_body( $response );

//decode
$data = json_decode( $data );
}
```

### Create Post
* This example uses basic authentication. A more secure authentication method should be used in production environments:

//set up post data
$post = array(
'title' => 'The Lord Of The Rings',
'post_type' => 'book',
'content' => 'Best book ever.'
);
//encode as JSON
$post = json_encode( $post );

//URL for posts route
$url = json_url( 'posts' );

//prepare headers with basic authentication
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ),
);

//prepare args for request
$arg = array (
'method' => 'POST',
'timeout' => 45,
'headers' => $headers,
'body' => $post
);

//create post
$response = wp_remote_post( $url, $args );

if ( is_wp_error( $response ) ) {
//it failed
$id = false;
}
else {
//success! get the ID of new post
$post = json_decode( wp_remote_retrieve_body( $response ) );
$id = $post[ 'id' ];
}
```

### Update A Post
* Change the title of an existing post, in this case, post ID 42

``` php

//set up post data
$post = array(
'title' => 'The Lord Of The Rings, Being the third part of the Lord of the Rings',
);
//encode as JSON
$post = json_encode( $post );

//URL for this post
$url = json_url( 'posts/42' );

//prepare headers with basic authentication
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'password' ),
);

//prepare args for request
$arg = array (
'method' => 'POST',
'timeout' => 45,
'headers' => $headers,
'body' => $post
);

//create post
$response = wp_remote_post( $url, $args );

if ( is_wp_error( $response ) ) {
//it failed
$id = false;
}
else {
//success! get the ID of new post
$post = json_decode( wp_remote_retrieve_body( $response ) );
$id = $post[ 'id' ];
}
```