Building a Custom REST API Endpoint in WordPress – A Step-by-Step Guide
If you’ve ever wanted to connect your WordPress site with an external app or service, creating a custom REST API endpoint is the best way to do it. As a developer, I often use WordPress REST APIs to send or receive data from other systems like mobile apps, SaaS platforms, or custom dashboards.
In this post, I’ll walk you through how to build a custom REST API endpoint in WordPress, step by step.
What is REST API in WordPress
WordPress comes with a built-in REST API that allows developers to interact with site data using simple HTTP requests (GET, POST, PUT, DELETE). For example, you can fetch posts or create users without logging into wp-admin.
But sometimes, you need custom endpoints maybe to integrate with your mobile app or to send data to a third-party CRM. That’s where creating your own endpoint helps.
Step 1: Register the Custom Endpoint
To create a REST API endpoint, we use the register_rest_route() function inside the rest_api_init hook.
Let’s say we want to create an endpoint to fetch user details.
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/user-info', [
'methods' => 'GET',
'callback' => 'get_custom_user_info',
'permission_callback' => '__return_true'
]);
});
function get_custom_user_info(WP_REST_Request $request) {
$user_id = get_current_user_id();
if (!$user_id) {
return new WP_Error('no_user', 'User not logged in', ['status' => 401]);
}
$user = get_userdata($user_id);
return [
'id' => $user->ID,
'name' => $user->display_name,
'email' => $user->user_email,
];
}
PHPNow you can access your endpoint from:https://yourdomain.com/wp-json/custom/v1/user-info
Step 2: Adding POST Method (For Sending Data)
If you want to receive data (for example, from a mobile app), use the POST method.
Let’s create an endpoint to save feedback data.
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/feedback', [
'methods' => 'POST',
'callback' => 'save_user_feedback',
'permission_callback' => '__return_true'
]);
});
function save_user_feedback(WP_REST_Request $request) {
$data = $request->get_json_params();
$feedback = sanitize_text_field($data['message']);
$user_id = get_current_user_id();
wp_insert_post([
'post_type' => 'feedback',
'post_title' => 'Feedback from ' . $user_id,
'post_content'=> $feedback,
'post_status' => 'publish'
]);
return ['success' => true, 'message' => 'Feedback received successfully!'];
}
PHPNow your external app can send POST requests to:https://yourdomain.com/wp-json/custom/v1/feedback
This will save the feedback data directly to WordPress.
Step 3: Adding Authentication
You shouldn’t make your endpoints public unless you must.
For secure endpoints, use the WordPress REST API authentication methods like:
- Basic Authentication (for testing)
- Application Passwords (for secure access)
- OAuth (for large-scale integrations)
For example, using Application Passwords, your external app can send requests safely with a username and generated password.
Step 4: Test Your Endpoint
To test your endpoint, you can use:
Example GET request in Postman:
GET https://yourdomain.com/wp-json/custom/v1/user-info
BashYou’ll see a JSON response like this:
{
"id": 2,
"name": "Kamal Hosen",
"email": "kamal@example.com"
}
JSONStep 5: Use Your Endpoint in an External App
Now that your endpoint works, you can connect it with any system.
For example:
- Send feedback from your mobile app to WordPress
- Fetch user data for a custom dashboard
- Connect your WooCommerce store data to another system
WordPress REST API is flexible you can integrate almost anything once you understand the basics.
Final Thoughts
Building a custom REST API endpoint in WordPress gives you complete control over how data moves between systems. Whether you’re connecting a mobile app, syncing a CRM, or building a headless WordPress setup custom endpoints make your life easier.
If you’re a WordPress developer looking to expand beyond themes and plugins, this is a great step forward.
