dev-master
9999999-dev https://github.com/Shelob9/jp-wp-api-rest-api-clientA simple client for working with the WordPress REST API via the WordPress HTTP API
GPL-2.0+
The Requires
by Josh Pollock
wordpress rest-api
A simple client for working with the WordPress REST API via the WordPress HTTP API
A simple client for creating and updating posts via the WordPress REST API via the WordPress HTTP API. Most of this code is based on articles I wrote for Torque., (*1)
Utilizes Mark Jaquith's WP-TLC-Transients Library. Requires WordPress and the WordPress REST API., (*2)
This is not a plugin., (*3)
The easy and best way to add it is to add "shelob9/jp-wp-rest-api-client": "dev-master"
to your site/plugin/theme's composer.json. Include composer autoloader., (*4)
Alternatively, add this repo to your site/plugin/theme using a Git Submodule or by employing the dark art of copypasta. Then manually include its file(s). Note: Caching isn't going to work unless you do a composer update or otherwise include Mark Jaquith's WP-TLC-Transients Library., (*5)
//get url string for posts in multiple CPTs with several filters $filters = array( 'posts_per_page' => 50, 'orderby' => 'modified_gmt', 'offset' => '10' ); $post_types = array( 'post', 'page', 'book', 'author' ); $url = JP_WP_REST_API_Client::posts_url_string( $post_types, $filters ); //make request $response = JP_WP_REST_API_Client::get_json( $url );
//get url string for posts in multiple CPTs with several filters $filters = array( 'posts_per_page' => 50, 'orderby' => 'modified_gmt', 'offset' => '10' ); $post_types = array( 'post', 'page', 'book', 'author' ); $url = JP_WP_REST_API_Client::posts_url_string( $post_types, $filters ); //make request $response = JP_WP_REST_API_Client::get_json_cached( $url );
Get All The Posts Leveraging Cache `$posts = JP_WP_REST_API_Client::get_json_cached( json_url() );```, (*6)
Copy Post From Remote Site To Local Site, (*7)
$url = 'http://remote_site.com/wp-json/5'; //get post $post = JP_WP_REST_API_Client::get_json( $url ); //insert to current site $new_post_id = JP_WP_REST_API_Client::insert_post_from_json( $post );
$url = json_url( 'posts/1' ); //get post $post = JP_WP_REST_API_Client::get_json( $url ); //make sure we got JSON if ( is_string( $post ) ) { //setup auth using basic auth $auth = 'Basic ' . base64_encode( 'username' . ':' . 'password' ); $response = JP_WP_REST_API_Client::remote_post( $post, $auth, 'http://remotesite.com/wp-json/posts' ); if ( ! is_wp_error( $response ) { //do something with correct response } }
Copyright 2014 Josh Pollock. Licensed under the terms of the GNU General public license version 2. Please share with your neighbor., (*8)
A simple client for working with the WordPress REST API via the WordPress HTTP API
GPL-2.0+
wordpress rest-api