diff --git a/examples/posts/jQuery-AJAX.md b/examples/posts/jQuery-AJAX.md new file mode 100644 index 0000000..e9bede5 --- /dev/null +++ b/examples/posts/jQuery-AJAX.md @@ -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!' ); + } + }); +``` diff --git a/examples/posts/php.md b/examples/posts/php.md new file mode 100644 index 0000000..b56fdae --- /dev/null +++ b/examples/posts/php.md @@ -0,0 +1,168 @@ +Requests to the API, from within WordPress, or any other HTTP request should use [the WordPress HTTP API](http://codex.wordpress.org/HTTP_API). + +When making requests to a the current site, URLs should be constructed using either the function `json_url()` or `get_json_url()` for a site in a multisite network other than the current site. 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, using these functions, which would return the most recent posts, would be constructed with `json_url( 'posts' );` + +The example code below use an undefined variable `$root_url` which should be the root URL for the site's API. + +### Get Most Recent Posts +* Number of posts will equal the posts_per_page option +``` php + //create url + $url = $base_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 = $base_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' + ); + + //construct URL + $url = $base_url . 'posts'; + $url = add_query_arg( $args, $url ); + + //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 = $base_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 = $base_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' ]; + } +``` + +