Mobile App Development with WordPress Backend: A Practical Guide to REST API Integration by Sanjay - HTML preview
Download the book in PDF, ePub, Kindle for a complete version.
04 13
Creating Custom
REST API Endpoints
When you build a mobile app that interacts with WordPress, you may need more
than just the default WordPress API endpoints. For example, you might want to
create custom endpoints to fetch specific data, perform certain actions, or handle
data in a unique way. This is where custom REST API endpoints come in. They allow
you to define your own routes and handle requests the way you need.
Why You Might Need Custom Endpoints
The default WordPress REST API provides standard endpoints for common tasks,
such as retrieving posts, users, or media. However, there are many situations where
you might need to:
Fetch data in a format that the default endpoints don’t support.
Perform specific actions (e.g., updating user information or
processing custom data).
Handle specific business logic or validation before data is returned
or processed.
Secure access to sensitive data by controlling which endpoints are
available to certain users.
14
By creating custom endpoints, you can ensure that your mobile app interacts with
WordPress exactly the way you need it to.
Steps to Create a Custom API Endpoint for Your Mobile App
To create a custom REST API endpoint, follow these steps:
1. Hook into the rest_api_init Action: WordPress has an action hook called
rest_api_init that runs when the REST API is initialized. You will use this
hook to register your custom endpoints.
2. Register the Endpoint Using register_rest_route: The register_rest_rout
function allows you to define a new endpoint and specify the route,
HTTP methods (GET, POST, etc.), and callback function that will handle
the request.
3. Define the Callback Function: The callback function is where you’ll handle
the logic for the custom endpoint. For example, you could query
the WordPress database, process data, or return a custom response.
Hooking into rest_api_init to Register a Custom Endpoint
To register a custom endpoint, you need to write a function that hooks into
rest_api_init and calls register_rest_route to create the endpoint. Below is an
example of how to do this.
Example Code for Registering a Basic API Endpoint that Fetches Posts
15
Here’s an example of how to send a request from your mobile app to get the token:

function custom_api_get_posts() {

register_rest_route( 'custom/v1', '/posts/', array(
'methods' => 'GET', // Define the HTTP method (GET in this case) 'callback' => 'get_posts_data', // Define the callback function that will handle the request ) );
}
function get_posts_data() {
// Query WordPress for posts
$posts = get_posts();
// Return the posts as a JSON response
return new WP_REST_Response($posts, 200); // 200 is the HTTP status code for success }
// Hook into the REST API initialization to register the endpoint add_action( 'rest_api_init', 'custom_api_get_posts' );
Explanation of the Code
register_rest_route( 'custom/v1', '/posts/', ... ): This line registers a new
route under the namespace custom/v1. The route /posts/ will handle the
GET requests for fetching posts.
'methods' => 'GET': This specifies that the endpoint will only accept
GET requests, meaning it will fetch data (not modify it).
'callback' => 'get_posts_data': The callback function get_posts_data
is defined below, and it will be called when the /posts/ endpoint is accessed.
get_posts_data(): This function queries the WordPress database for
posts using the get_posts() function, and then returns the posts as a
JSON response using WP_REST_Response. The HTTP status code 200
means the request was successful.
16
add_action( 'rest_api_init', 'custom_api_get_posts' ): This tells
WordPress to run the custom_api_get_posts function when the
REST API is initialized, which registers the new route.
How to Use This Endpoint
Once the code is added to your WordPress site, you can access this custom
endpoint by visiting:
This will return a JSON response with a list of posts from your WordPress site. You
can now use this endpoint in your mobile app to fetch data dynamically.
