Book Image

WordPress Web Application Development

By : Rakhitha Nimesh Ratnayake
Book Image

WordPress Web Application Development

By: Rakhitha Nimesh Ratnayake

Overview of this book

Table of Contents (19 chapters)
WordPress Web Application Development Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a custom API


Custom APIs are essential for adding web application-specific behaviors, which go beyond the generic blogging functionality. We need the implementation for both the server and client to create a custom API. Here, we will build an API function that outputs the list of developers in the portfolio application. Here, we will use a separate plugin for API creation as an API is usually a separate component from the application. Let's get started by creating another plugin folder called wpwa-xml-rpc-api with the main file called class-wpwa-xml-rpc-api.php.

Let's look at the initial code to build the API server:

class WPWA_XML_RPC_API {
  public function __construct() {
    add_filter('xmlrpc_methods', array($this, 'xml_rpc_api'));
  }
  public function xml_rpc_api($methods) {
    $methods['wpwa.getDevelopers'] = array($this, 'developers_list');
    return $methods;
  }
}
new WPWA_XML_RPC_API();

First, we use the plugin constructor to add the WordPress filter called xmlrpc_methods...