Md Mominul Islam | Software and Data Enginnering | SQL Server, .NET, Power BI, Azure Blog

while(!(succeed=try()));

LinkedIn Portfolio Banner

Latest

Home Top Ad

Responsive Ads Here

Sunday, August 23, 2026

WordPress REST API Custom Endpoint Returns 404 – register_rest_route Fix Guide

WordPress REST API Custom Endpoint Returns 404 – register_rest_route Fix Guide 2026 | FreeLearning365
🌍 World Newspaper Hub Read 500+ Global Newspapers Online — Free
Explore Now
🎯 Job Interview Preparation Programming · Cloud · Data · ERP & More
Go to Interview Portal
Home / WordPress / REST API

WordPress REST API Custom Endpoint Returns 404 – register_rest_route Fix Guide

August 23, 2026 9 min read REST API WordPress Troubleshooting

🚧 Why Your Custom REST API Endpoint Returns 404

You’ve registered a custom endpoint using register_rest_route(), but when you try to access it, WordPress returns a 404 Not Found error. This is a common frustration for WordPress developers. The issue usually lies in the registration hook, permalink structure, or the request method.

Goal: This guide walks you through every possible cause and solution — from proper hook usage to flushing rewrite rules and testing your endpoint.

🔍 Common Causes of 404 on Custom Endpoints

  • register_rest_route called too early or too late – Must be hooked to rest_api_init.
  • Permalinks not flushed – WordPress needs to rebuild rewrite rules after adding new routes.
  • Missing namespace or route path – The endpoint URL does not match the registered route.
  • HTTP method mismatch – Using POST when the route only accepts GET (or vice versa).
  • Authentication or permission callback – If the permission callback returns false, the endpoint may appear as 404.
  • Plugin/theme conflict – Another plugin overrides the route or disables the REST API.
  • Server configuration – ModSecurity or custom rewrite rules may block the request.

📝 Proper Usage of register_rest_route

The function must be hooked to rest_api_init. Here’s a minimal working example:

functions.php (or plugin) add_action('rest_api_init', function () { register_rest_route('myplugin/v1', '/test/', array( 'methods' => 'GET', 'callback' => 'myplugin_test_callback', )); }); function myplugin_test_callback($request) { return new WP_REST_Response(array('message' => 'Hello World!'), 200); }

The endpoint will be available at: https://yoursite.com/wp-json/myplugin/v1/test/

Common mistake: Calling register_rest_route directly in the root of the file without the rest_api_init hook. This will cause the route to be registered too early and be ignored.

After registering a new route, you must flush rewrite rules so WordPress knows about the new endpoint. You can do this by:

  • Going to Settings → Permalinks and clicking Save Changes (even without changes).
  • Programmatically: flush_rewrite_rules(); (but avoid calling it on every page load — use it once during plugin activation).
Plugin activation hook register_activation_hook(__FILE__, 'myplugin_activate'); function myplugin_activate() { // Register routes then flush add_action('rest_api_init', 'myplugin_register_routes'); flush_rewrite_rules(); }

If you’re testing on a local environment, simply visiting the Permalinks page is the quickest fix.

🔎 Debugging Steps

1. Check if REST API is enabled

Visit /wp-json/ on your site. If you see a JSON response with routes, the REST API is working. If you get a 404, your permalinks may be set to plain (e.g., ?p=123). Switch to a pretty permalink structure.

2. Verify the route is registered

Use the rest_get_server() to list all registered routes:

$server = rest_get_server(); $routes = $server->get_routes(); if (isset($routes['/myplugin/v1/test'])) { echo 'Route is registered!'; } else { echo 'Route not found.'; }

If the route is not in the list, your registration didn’t work — check the hook and namespace.

3. Enable WP_DEBUG

Add define('WP_DEBUG', true); to wp-config.php to see any PHP errors that might prevent the route from being registered.

4. Check the permission callback

If you have a permission callback that returns false, the endpoint may return a 404 or 403. Ensure your callback returns true or WP_Error for proper handling.

📬 Testing with Postman / cURL

Use tools like Postman or cURL to test your endpoint:

cURL example curl -X GET https://yoursite.com/wp-json/myplugin/v1/test/

If you get a 404, double‑check the URL structure. If you get a 200 with the expected data, your endpoint works.

✅ Best Practices for Custom REST Endpoints

  • Always use the rest_api_init hook to register routes.
  • Flush rewrite rules after adding new routes (preferably on plugin activation).
  • Use a unique namespace (e.g., myplugin/v1) to avoid conflicts.
  • Provide a permission callback to secure your endpoints.
  • Validate and sanitize input using sanitize_text_field and rest_ensure_request.
  • Return a WP_REST_Response with proper status codes.
  • Test thoroughly with different HTTP methods and parameters.
  • Keep your plugin/theme updated to avoid compatibility issues.
Frequently Asked Questions

Explore More Free Learning Resources

World Newspaper Hub
500+ global newspapers online
Read Now
Job Interview Prep
Programming, Cloud, Data & ERP
Explore
Learn Free
JS, Python, SQL, AI & more
Start Learning
100+ Free Tools
Dev, SEO, daily utilities
Use Tools
eBook Collection
Free downloads for everyone
Download
Free Question Bank
BCS, HSC, SSC, JSC, PSC
Solve Now
AI Background Remover
Remove image bg instantly
Try Now
Barcode & Label Generator
Custom barcodes, QR, A4 sheets
Generate
QR Code Generator
Custom QR codes online
Create
AI Prompt Generator
40+ professional prompt types
Explore
Professional Training
Advance your IT career
Learn More
🎯 Job Interview Preparation Programming · Cloud · Data · ERP & More
Go to Interview Portal

© 2026 FreeLearning365.com • Built with for learners worldwide.
All rights reserved. Content may not be reproduced without permission.

No comments:

Post a Comment

Thanks for your valuable comment...........
Md. Mominul Islam