-
Notifications
You must be signed in to change notification settings - Fork 17
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
Shelob9
wants to merge
2
commits into
WP-API:master
Choose a base branch
from
Shelob9:example-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' ); | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' );` | ||
|
||
### 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' ]; | ||
} | ||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.