📖 Scenario: You are building a WordPress site and want to add a new custom endpoint to the REST API. This endpoint will provide a simple message when accessed.
🎯 Goal: Create a custom REST API endpoint in WordPress that returns a JSON message.
📋 What You'll Learn
Create a function to register a custom REST API route
Use the register_rest_route function with a namespace and route
Add a callback function that returns a JSON response
Hook the registration function to the rest_api_init action
💡 Why This Matters
🌍 Real World
Custom endpoints let you extend WordPress REST API to provide your own data or functionality for apps or frontend JavaScript.
💼 Career
Knowing how to register custom endpoints is useful for WordPress developers building plugins, themes, or headless WordPress applications.
Progress0 / 4 steps
1
Create the custom endpoint registration function
Create a function called my_custom_endpoint that will be used to register the custom REST API route.
Wordpress
Hint
Start by defining a function named my_custom_endpoint with empty body.
2
Add the REST route registration inside the function
Inside the my_custom_endpoint function, use register_rest_route to register a route with namespace 'myplugin/v1' and route 'hello'. The route should accept GET requests and use a callback function named my_custom_endpoint_callback.
Wordpress
Hint
Use register_rest_route with the exact namespace and route strings, and set the methods and callback keys in the array.
3
Create the callback function for the endpoint
Create a function called my_custom_endpoint_callback that returns an array with a key message and value 'Hello from custom endpoint!'.
Wordpress
Hint
Define a function named my_custom_endpoint_callback that returns the specified array.
4
Hook the registration function to the REST API initialization
Add an action hook that calls my_custom_endpoint on the rest_api_init action.
Wordpress
Hint
Use add_action with the rest_api_init hook and the function name my_custom_endpoint.
Practice
(1/5)
1. What is the main purpose of using add_rewrite_endpoint in WordPress?
easy
A. To enqueue styles and scripts
B. To register a new plugin in WordPress
C. To add a new URL part (endpoint) for custom content or actions
D. To create a new database table
Solution
Step 1: Understand the function purpose
add_rewrite_endpoint is used to add new URL parts to WordPress URLs.
Step 2: Identify correct usage context
This function helps create custom endpoints for special content or actions, not for plugins, tables, or scripts.
Final Answer:
To add a new URL part (endpoint) for custom content or actions -> Option C
Quick Check:
add_rewrite_endpoint adds URL parts = D [OK]
Hint: Endpoints add URL parts for custom content [OK]
Common Mistakes:
Confusing endpoints with plugin registration
Thinking it creates database tables
Assuming it loads scripts or styles
2. Which hook is the correct place to call add_rewrite_endpoint when registering a custom endpoint?
easy
A. wp_footer
B. wp_enqueue_scripts
C. admin_menu
D. init
Solution
Step 1: Recall WordPress hook usage
The init hook runs early and is the recommended place to add rewrite rules and endpoints.
Step 2: Eliminate incorrect hooks
wp_enqueue_scripts is for scripts/styles, admin_menu is for admin menus, and wp_footer runs late in page rendering.
Final Answer:
init -> Option D
Quick Check:
add_rewrite_endpoint called on init hook = A [OK]
Hint: Use init hook for rewrite endpoints [OK]
Common Mistakes:
Using wp_enqueue_scripts instead of init
Adding endpoints in admin_menu hook
Placing code in wp_footer hook
3. Given this code snippet, what will be the effect on the URL structure?
B. Rewrite rules are not flushed after adding the endpoint
C. The hook 'init' is incorrect for this function
D. The constant EP_ROOT cannot be used with add_rewrite_endpoint
Solution
Step 1: Check endpoint registration correctness
The endpoint name 'promo' is valid, and EP_ROOT is a valid location constant.
Step 2: Identify missing step
After adding endpoints, rewrite rules must be flushed (usually once) to apply changes; this code does not flush rules.
Final Answer:
Rewrite rules are not flushed after adding the endpoint -> Option B
Quick Check:
Flush rewrite rules after adding endpoints = C [OK]
Hint: Flush rewrite rules after adding endpoints [OK]
Common Mistakes:
Skipping rewrite rules flush
Using wrong hook instead of init
Thinking endpoint names must be special
5. You want to add a custom endpoint 'download' that works on posts and pages, and you want to flush rewrite rules only once after activation. Which code snippet correctly achieves this?