You are previewing Learning WordPress REST API.
O'Reilly logo
Learning WordPress REST API

Book Description

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

About This Book

  • 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

  • Who This Book Is 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

  • In Detail

    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.

    Style and Approach

    A step by step tutorial to explain the new and exciting world of the WordPress REST API with the real world examples.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Learning WordPress REST API
      1. Learning WordPress REST API
      2. Credits
      3. About the Authors
      4. Acknowledgments
      5. About the Reviewer
        1. Why subscribe?
        2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with REST API
        1. Introducing REST API
          1. Defining API
          2. Defining REST
          3. Defining JSON
        2. Using REST API in real-world applications
          1. Advantages of REST services
        3. Key considerations when working with REST
          1. Architectural components in REST
          2. Design principles in REST
        4. Getting started with REST implementation
          1. Passing commands in SOAP versus REST
          2. Handling data in REST
        5. Using REST in different programming languages
          1. Ruby
          2. Python
          3. Perl
          4. C#
          5. Java
          6. PHP
          7. JavaScript
        6. REST API in WordPress
          1. Universality
          2. Remote management
          3. Third-party support
        7. Summary
      9. 2. Interacting with REST API in WordPress
        1. Getting started
        2. Issuing requests via Postman
        3. HTTP API in WordPress
        4. Fetching GET post output in JSON objects
          1. Issuing queries
        5. Interacting REST API via PHP
          1. Explanation of function
        6. Issuing POST requests
          1. Implementing GET meta fields using REST API in WordPress
          2. Implementing POST meta fields using REST API in WordPress
        7. Summary
      10. 3. Working with Taxonomies and Users with REST API
        1. Working with taxonomies in WordPress using REST API
          1. Basics
          2. HTTP requests
        2. Implementing REST API and JavaScript with taxonomies
          1. How to send GET requests for taxonomies
          2. How to send POST requests for taxonomies
        3. Working with users in WordPress using REST API
          1. How to GET user data using REST API in WordPress
          2. GET public user data
          3. GET privileged user data
          4. How to POST (and edit) user data using REST API in WordPress
        4. Summary
      11. 4. Working with Forms Using REST API
        1. Overview
        2. Fundamentals
        3. Setting up the plugin
          1. Creating the form with HTML markup
          2. Enqueueing your JavaScript file
          3. Issuing requests for creating posts
          4. OAuth authorization method
          5. Issuing requests for editing posts
        4. Summary
      12. 5. Custom Routes in WordPress REST API
        1. Overview
        2. Adding custom routes to WordPress REST API
        3. Setting up the custom route
          1. Route namespace
          2. URL after namespace
          3. Endpoints to a route
          4. Optional Boolean argument
        4. Setting up the custom endpoints
          1. Transport method
          2. Defining our fields
          3. The callback function
            1. The permissions callback
        5. Processing requests (and responses)
          1. WP_Query
          2. WPDB
          3. get_post_meta
          4. Third-party plugins
        6. Summary
      13. 6. Creating a Simple Web App using WordPress REST API
        1. Overview
        2. Setting up your WordPress site
          1. Cross origin problems and bugs
          2. Handling multiple requests
          3. Optimization measures
        3. Steps to disable the default routes
        4. More about WP REST API
        5. The REST architecture
          1. HTTP verbs
          2. Endpoints and routes
          3. JSON REST API for WordPress
          4. JSON REST APIs in WordPress
          5. WP REST API at the moment
          6. Tools
          7. Installing the plugin
        6. Going further
        7. WP REST API – setting up and using basic authentication
          1. Authentication
          2. WP REST API authentication
          3. Basic authentication
          4. Installing the plugin
          5. Postman requests
          6. Authenticated requests from the command line
        8. JavaScript authenticated requests
        9. WP HTTP API for authenticated requests
          1. Status check
        10. WP REST API - setting up and using advanced authentication
          1. OAuth authentication
          2. OAuth security concerns
          3. OAuth 2.0
          4. OAuth authentication flow
            1. Oauth_callback function
            2. OAuth_verifier function
          5. OAuth installation
        11. Assessing the availability of the OAuth API
          1. Application management
          2. Generating OAuth credentials
          3. User authorization
          4. Token exchange
          5. Status check
        12. WP REST API - retrieving data
          1. The GET request
          2. Options request
          3. Retrieving posts from the server
          4. The filter[] syntax
          5. Post revisions, categories, tags, and meta
          6. Other resources
          7. Status check
        13. WP REST API: creating and editing posts
          1. CRUD methods in routes
          2. Creating and updating posts
          3. Creating and updating post meta
          4. Creating and updating data
          5. Sending data as URL parameters
          6. Sending data as a JSON object
          7. Sending data using forms
          8. Uploading media via multipart/form-data
          9. Deleting data
          10. Status check
        14. WP REST API: internals and customization
          1. Internal classes and methods of WP REST API
          2. Infrastructure classes
          3. WP_REST_Server
          4. WP_REST_Request
          5. WP_REST_Response
          6. Modifying server responses
          7. The register_rest_field()
        15. Summary
      14. 7. Mastering REST API for Your Projects
        1. Backward compatibility
        2. A universal API
        3. Architectural structure
        4. REST architectural constraints
          1. The formal REST constraints
            1. Stateless
            2. Client-server
            3. Layered system
            4. Cacheable
            5. Code on demand
            6. Uniform interfaces
              1. Resource identification
              2. Representation and resources
            7. Self-descriptive messages
            8. HATEOS
        5. Ever-growing REST API
        6. REST API as a platform
          1. Implementing REST API in apps
          2. Custom data types in WordPress
        7. REST API in later versions of WordPress
        8. REST API and WordPress plugin development
        9. REST API-based authentication
          1. OAuth authentication
          2. Basic authentication
          3. Cookie authentication
        10. REST API and security
        11. REST API being used in WordPress plugins
          1. Overview
        12. Disabling REST API
        13. Summary
      15. 8. WordPress REST API in Practice
        1. Key differences between v1 and v2 of the plugin
        2. Internal changes
        3. External changes
        4. Future changes
        5. Functions of APIs
        6. The REST API in theory
        7. A guide to HTTP and REST
          1. HTTP
          2. HTTP client libraries
        8. The WordPress REST API
        9. The JSON REST API
        10. Developers of WP REST API
          1. Overall description
          2. JSON
          3. JavaScript
          4. WP REST API and JavaScript
          5. XML-RPC in WordPress
        11. REST API revenue sources
          1. Mobile applications
          2. Wearable devices
          3. SaaS services
          4. Third-party apps
          5. Web services
        12. Competing architectures on the Web
          1. RESTful architectures
          2. RPC architectures
          3. Combination of REST and RPC
          4. Overview of architectures
        13. JavaScript and WordPress
        14. AJAX in WordPress
        15. Things to consider when using REST API
          1. Interacting with databases using REST API
          2. Doing more with REST API
        16. Current status of REST API
          1. WordPress features
          2. REST API conclusion
        17. Progressive enhancement of WP REST API
        18. WordPress Calypso and the REST API
        19. Securing a REST API
          1. OAuth protocol
        20. WordPress API and regular users
        21. Building your own API
        22. Drawbacks of a custom API
          1. REST API management
          2. Implementation of custom services for REST API
          3. Integration of REST API with mobile applications
          4. Standards for custom REST APIs
        23. Custom API tokens
        24. Summary
      16. 9. Summing It Up
        1. Comparison of REST API with XML-RPC
          1. RPC versus REST
          2. Keypoints
          3. Disadvantages of XML-RPC
        2. XML-RPC usage in WordPress
          1. Usage of XML RPC
        3. REST API token-based authentication
        4. Making sure your apps and sites are backward-compatible
          1. Backward compatibility in practice
            1. New functionality
            2. Preventing script breaking
        5. The future of REST API
          1. What will happen with REST in the future?
            1. PHP and WordPress
            2. Mobile integration
            3. The backend
          2. REST API plugin versions
          3. Goals for REST API
        6. Limitations of REST API
        7. Summary