Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Learning WordPress REST API
Learning WordPress REST API

Learning WordPress REST API: A practical tutorial to get you up and running with the revolutionary WordPress REST API

Arrow left icon
Profile Icon Sufyan bin Uzayr Profile Icon Mathew Rooney
Arrow right icon
$23.39 $25.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
eBook Jul 2016 216 pages 1st Edition
eBook
$23.39 $25.99
Paperback
$26.39 $32.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Sufyan bin Uzayr Profile Icon Mathew Rooney
Arrow right icon
$23.39 $25.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
eBook Jul 2016 216 pages 1st Edition
eBook
$23.39 $25.99
Paperback
$26.39 $32.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$23.39 $25.99
Paperback
$26.39 $32.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning WordPress REST API

Chapter 2. Interacting with REST API in WordPress

In the previous chapter, we became familiar with the basics of REST API, how RESTful services work, and how to issue and manage basic REST commands in different languages and using different methods.

Now that the introduction is out of the way, it is time for us to actually get started with REST API in WordPress. In the last chapter, we have seen the benefits of using REST API in WordPress and all that it can bring to the table in terms of features.

Starting from this chapter, we will now be seeing how to use REST API when working with WordPress. This chapter will introduce you to basic GET and POST requests and then will teach you how to deal with posts in WordPress via REST API. Furthermore, you will also learn how to handle posts, post metadata or meta fields, and then copy posts from one remote site to another.

Getting started

First up, you will need to set up your WordPress website. Obviously, you should not use a production site for learning purposes; therefore, I will strongly advise you to set up a test installation of WordPress for experimenting and playing with REST API. Depending on your mode of operation, you may choose to do it the way it suits you—some developers prefer having a local version of WordPress running on their device, whereas others, such as myself, set up WordPress live on a test server and access it accordingly.

You might also do it via Vagrant, if that suits you.

You may then install the WordPress REST API plugin much like any other normal plugin. Find the latest version at https://wordpress.org/plugins/rest-api/.

That said, let us get started with REST requests in WordPress. As we have seen in the last chapter, REST requests generally revolve around the four common HTTP transport methods: GET, PUT, POST, and DELETE. Plus, we have also learned that GET and POST requests...

Issuing requests via Postman

The biggest and most obvious advantage of Postman is that it allows you to turn requests into code snippets that you can use and reuse within your code. Thus, Postman can be used to export requests as JavaScript, and that makes it the perfect fit when working with REST API for WordPress or web development.

Postman lets you send authenticated requests in a native manner. In Google Chrome, once you have installed and activated the Postman extension, you can start sending HTTP requests.

Postman supports multiple HTTP requests, and you can see that directly in the drop-down menu.

Issuing requests via Postman

Of course, for our purpose, the GET and POST requests are the most important.

To issue an HTTP request via Postman, you need to enter the URL value and specify the parameters, if any. For instance, a GET request to a sample URL would look like as shown in the following screenshot:

Issuing requests via Postman

The preceding requests give us raw response in HTML code. You can also see the same response in JSON, XML, or text...

HTTP API in WordPress

As the name suggests, in WordPress, the HTTP API can be used to simplify HTTP requests. It can let you make HTTP requests via PHP, either to the same site or to a different site. But more importantly, HTTP API in WordPress lets you transform URL strings into JSON objects.

Consider the following URL string: http://example.com/wp-json/wp/v2/posts.

It is like any other URL on the Internet. Now, with HTTP API, we can convert it into a JSON object, making use of the wp_remote_get () function from the WordPress core:

$json = wp_remote_get ( 'http://example.com/wp-json/wp/v2/posts' ); 

Now, $json will yield an array, and that is precisely the response that we need.

To understand it better, let us now put together a very small function that accepts a URL string and then gives an array of post objects:

$response = wp_remote_get( $url ); 
function get_json( $url ) { 
//GET remote site 
$response = wp_remote_get( $url ); 
//Checking for errors 
if ( is_wp_error( $response...

Fetching GET post output in JSON objects

So far, we have seen how to GET posts and JSON objects. The preceding queries are sufficient to fetch (or GET) data for you, but how will you output the posts?

In WordPress, we often output posts by using the get_post() function that uses the global $post object. In a similar manner, we can use a loop that runs through all the posts retrieved by REST API and outputs them accordingly. For example, consider the following code:

$url = add_query_arg( 'per_page', 10, rest_url() ); 
$posts = get_json( $posts ); 
if ( ! empty( $posts ) ) { 
  foreach( $posts as $post ) { ?> 
  <article id="<?php echo esc_attr($post->ID ); ?>"> 
    <h1><?php echo $post->title; ?></h1> 
    <div><?php wpautop( $post->content ); ?></div> 
 
  </article> 
<?php } //foreach 
} 

Looking at the outcome of this loop when used within a standard function, this is how it will work (the test site...

Getting started


First up, you will need to set up your WordPress website. Obviously, you should not use a production site for learning purposes; therefore, I will strongly advise you to set up a test installation of WordPress for experimenting and playing with REST API. Depending on your mode of operation, you may choose to do it the way it suits you—some developers prefer having a local version of WordPress running on their device, whereas others, such as myself, set up WordPress live on a test server and access it accordingly.

You might also do it via Vagrant, if that suits you.

You may then install the WordPress REST API plugin much like any other normal plugin. Find the latest version at https://wordpress.org/plugins/rest-api/ .

That said, let us get started with REST requests in WordPress. As we have seen in the last chapter, REST requests generally revolve around the four common HTTP transport methods: GET, PUT, POST, and DELETE. Plus, we have also learned that GET and POST requests...

Issuing requests via Postman


The biggest and most obvious advantage of Postman is that it allows you to turn requests into code snippets that you can use and reuse within your code. Thus, Postman can be used to export requests as JavaScript, and that makes it the perfect fit when working with REST API for WordPress or web development.

Postman lets you send authenticated requests in a native manner. In Google Chrome, once you have installed and activated the Postman extension, you can start sending HTTP requests.

Postman supports multiple HTTP requests, and you can see that directly in the drop-down menu.

Of course, for our purpose, the GET and POST requests are the most important.

To issue an HTTP request via Postman, you need to enter the URL value and specify the parameters, if any. For instance, a GET request to a sample URL would look like as shown in the following screenshot:

The preceding requests give us raw response in HTML code. You can also see the same response in JSON, XML, or...

HTTP API in WordPress


As the name suggests, in WordPress, the HTTP API can be used to simplify HTTP requests. It can let you make HTTP requests via PHP, either to the same site or to a different site. But more importantly, HTTP API in WordPress lets you transform URL strings into JSON objects.

Consider the following URL string: http://example.com/wp-json/wp/v2/posts.

It is like any other URL on the Internet. Now, with HTTP API, we can convert it into a JSON object, making use of the wp_remote_get () function from the WordPress core:

$json = wp_remote_get ( 'http://example.com/wp-json/wp/v2/posts' ); 

Now, $json will yield an array, and that is precisely the response that we need.

To understand it better, let us now put together a very small function that accepts a URL string and then gives an array of post objects:

$response = wp_remote_get( $url ); 
function get_json( $url ) { 
//GET remote site 
$response = wp_remote_get( $url ); 
//Checking for errors 
if ( is_wp_error...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn how to run the latest WordPress REST API with various platforms
  • Create exciting apps and manage non-WordPress content with them
  • Secure, export, and manage your data through illustrative examples

Description

The WordPress REST API is a recent innovation that has the potential to unlock several new opportunities for WordPress developers. It can help you integrate with technologies outside of WordPress, as well as offer great flexibility when developing themes and plugins for WordPress. As such, the REST API can make developers’ lives easier. The book begins by covering the basics of the REST API and how it can be used along with WordPress. Learn how the REST API interacts with WordPress, allowing you to copy posts and modify post metadata. Move on to get an understanding of taxonomies and user roles are in WordPress and how to use them with the WordPress REST API. Next, find out how to edit and process forms with AJAX and how to create custom routes and functions. You will create a fully-functional single page web app using a WordPress site and the REST API. Lastly, you will see how to deal with the REST API in future versions and will use it to interact it with third-party services. By the end of the book, you will be able to work with the WordPress REST API to build web applications.

Who is this book for?

This book is for WordPress developers and designers who want to get a complete practical understanding of the WordPress REST API and leverage it to create fully-featured web apps.

What you will learn

  • * Use the WordPress REST API to read, write, and edit posts
  • * Create and work with metadata using the WordPress REST API
  • * Work with taxonomies using the REST API
  • * Add custom routes and build apps using the WordPress REST API
  • * Process requests and integrate with external applications and frameworks
  • * Make your WordPress projects ready for the RESTful API standard

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 28, 2016
Length: 216 pages
Edition : 1st
Language : English
ISBN-13 : 9781786460233
Vendor :
WordPress Foundation
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 28, 2016
Length: 216 pages
Edition : 1st
Language : English
ISBN-13 : 9781786460233
Vendor :
WordPress Foundation
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 110.57 125.97 15.40 saved
WordPress Plugin Development Cookbook
$35.19 $43.99
WordPress Web Application Development - Second Edition
$48.99
Learning WordPress REST API
$26.39 $32.99
Total $ 110.57 125.97 15.40 saved Stars icon

Table of Contents

9 Chapters
1. Getting Started with REST API Chevron down icon Chevron up icon
2. Interacting with REST API in WordPress Chevron down icon Chevron up icon
3. Working with Taxonomies and Users with REST API Chevron down icon Chevron up icon
4. Working with Forms Using REST API Chevron down icon Chevron up icon
5. Custom Routes in WordPress REST API Chevron down icon Chevron up icon
6. Creating a Simple Web App using WordPress REST API Chevron down icon Chevron up icon
7. Mastering REST API for Your Projects Chevron down icon Chevron up icon
8. WordPress REST API in Practice Chevron down icon Chevron up icon
9. Summing It Up Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
(2 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 100%
Amazon Customer Jun 08, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Not a book for learning the API. Very poor description about what can be done and how. Lots of promises to show you later and lots of repetition.
Amazon Verified review Amazon
PapyEt Jun 04, 2018
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
ne vous fiez pas au contenu présenté en couverture...très grosse déception... il n'y a pas grand chose dans ce livre; un conseil cherchez des tutos sur internet vous serez mieux formés
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon