You've already learned how to create basic WordPress plugins in this very chapter, so now, let's combine this knowledge with the new addition of the REST API. The plugin we're building is going to simply display a list of posts from another blog based on a shortcode.
Let's have a look at the full code of this plugin—all contained within a single file named kk_rest_demo.php, as follows:
function kk_rest_handler($atts, $content=null) { extract(shortcode_atts(array( 'website_domain' => 'newinternetorder.com', 'how_many' => '3' ), $atts)); $response = wp_remote_get( 'http://' . $website_domain . '/wp- json/wp/v2/posts/' ); if( is_wp_error( $response ) ) { $error_string = $response->get_error_message(); return 'Error occurred: <em>' . $error_string . '</em>'; } $posts = json_decode( wp_remote_retrieve_body( $response ) ); if( empty( $posts ) ) { return 'No posts found'; } else { $result = '<ul>'; $post_count = 0; foreach( $posts as $post ) { $post_count++; if ($post_count <= $how_many) { $result .= '<li><a href="' . $post->link. '">' . $post->title->rendered . '</a></li>'; } } $result .= '</ul>'; return $result; } } add_shortcode('kk_rest', 'kk_rest_handler');
The first function call, extract(), is something we know from the previous shortcode plugin/widget. It extracts the attributes given to the shortcode. In our case, the shortcode works with two optional attributes, as follows:
- website_domain: This indicates the domain name of the WordPress site that the plugin should communicate with (defaults to newinternetorder.com).
- how_many: This indicates how many posts should be fetched (defaults to 3).
The next function call is where the main REST API communication happens. This call references a given route and fetches blog posts using the GET endpoint (by default), as follows:
$response = wp_remote_get( 'http://' . $website_domain . '/wp-json/wp/v2/posts/' );
After that, we just need to check there weren't any errors, and if so, halt the function. The next function call decodes the response and allows us to reference individual posts one by one later on, as follows:
$posts = json_decode( wp_remote_retrieve_body( $response ) );
The final foreach loop and if clause go through each post and retrieve their title and URL, which then get added to a standard HTML-unordered list and returned by the shortcode. At this point, and once I activate this plugin on my test site, I can create a new post and add the following shortcode to it:
[kk_rest website_domain="newinternetorder.com" how_many="2"] [/kk_rest]
This tells the plugin to communicate with newinternetorder.com and fetch the two most recent posts. The following screenshot shows the result of this as visible on the frontend:
Of course, the plugin we've built here is very simple, and it does not do anything in terms of making sure that the domain provided is valid, or anything else security-related. However, the purpose of this demo is only to show you the simplest way of working with the REST API, and nothing else. Once again, I strongly encourage you to review some of the official resources, such as the REST API Handbook at https://developer.wordpress.org/rest-api/.