Learn how to use register_rest_route to build custom WordPress endpoints. Master namespaces, route parameters, and verification to power your plugin.
Previously in this course, we covered Localizing Data for JavaScript, which ensured our frontend had the necessary environment variables to communicate with the server. Now that our environment is ready, we need to actually build the "server-side" destination for those requests.
Understanding the anatomy of a WordPress REST API endpoint is the single most important step in decoupling your plugin's UI from its data logic.
In WordPress, the REST API acts as a bridge between your database and your React-based admin dashboard. When you register a new endpoint, you aren't just creating a URL; you are defining a contract.
A WordPress endpoint is defined by three core components:
kb/v1) that prevents collisions with other plugins or core routes./items or /items/(?P<id>\d+)).The heart of this process is the register_rest_route() function. It must be hooked into the rest_api_init action to ensure your endpoints are registered only when the API is initialized.
Here is the basic structure for our Knowledge Base plugin:
PHPadd_action( 'rest_api_init', function () { register_rest_route( 'kb/v1', '/items', array( 'methods' => 'GET', 'callback' => 'kb_get_items', 'permission_callback' => '__return_true', #6A9955">// Caution: Use real checks later )); }); function kb_get_items( $request ) { return new WP_REST_Response( array( 'message' => 'Hello from the API!' ), 200 ); }
Namespaces should follow the plugin-name/version convention. This keeps your API organized and versioned, which is critical for long-term maintenance.
Route parameters allow for dynamic endpoints. If you need to fetch a specific item, you use regex syntax within the route string:
PHPregister_rest_route( 'kb/v1', '/items/(?P<id>\d+)', array( 'methods' => 'GET', 'callback' => 'kb_get_single_item', ));
The (?P<id>\d+) syntax tells WordPress: "Expect a digit-only parameter here, and make it available in the $request object as id." You can then access this in your callback via $request['id'].
Once you have registered your route, you can verify it exists without writing a single line of JS.
your-site.test/wp-json/.kb/v1).If your route is registered correctly, you will see it listed in the routes object of the index. This confirms that WordPress has successfully indexed your endpoint.
rest_api_init hook.kb/v1/status.GET and return an array containing ['status' => 'online'].your-site.test/wp-json/kb/v1/status in your browser and confirm you see the JSON response.rest_api_init: If you try to call register_rest_route outside of this action, the API won't know your route exists.permission_callback: In newer versions of WordPress, omitting the permission_callback will throw a warning. Always define one, even if it is just __return_true while you are in the initial development phase. (We will cover secure permission checks in the next lesson).We’ve moved from configuration to construction. By utilizing register_rest_route, we've established the entry point for our plugin's data. We've defined a namespace, mapped a route with parameters, and verified the output via the wp-json discovery endpoint. This infrastructure is exactly what we need to start building out robust, secure REST API Integration: Exposing Data for External Consumption.
Up next: We will secure these endpoints by implementing proper REST API permission callbacks.
Learn to update existing WordPress resources using REST API PUT and PATCH methods. Master ID-based routing and secure data modification for your plugins.
Read moreMaster the WordPress REST API by creating POST endpoints. Learn to extract request bodies, sanitize data, and insert new posts into the database securely.
Anatomy of a REST API Endpoint
Implementing CRUD in the Admin UI
Understanding WordPress Data Store Architecture
Registering a Custom Data Store
Writing Selectors for Data Access
Defining Actions and Reducers
Implementing Resolvers for Data Fetching
Optimizing Performance with Selectors
Handling Complex State Dependencies
Implementing Nonce Verification
Advanced Sanitization Techniques
Input Validation and Error Handling
Protecting Admin Screens
Production Build Pipeline
Debugging React in the WordPress Admin
Building Search and Filter Functionality
Internationalization in React
Managing File Uploads via REST API
Optimizing API Response Times
Working with Date and Time in React
Implementing Drag-and-Drop Sorting
Creating Custom Hooks for API Logic
Integrating with Gutenberg Blocks
Handling Conflict Resolution
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web