A custom endpoint is a new URL path added to the WordPress REST API that allows you to create your own API routes to handle specific data or actions beyond the default WordPress endpoints.
Click to reveal answer
beginner
Which WordPress function is used to register a custom REST API endpoint?
The function register_rest_route() is used to add a custom endpoint to the WordPress REST API.
Click to reveal answer
intermediate
What are the main parameters of register_rest_route()?
The main parameters are:
namespace: a string to group your routes (e.g., 'myplugin/v1')
route: the endpoint path (e.g., '/data')
args: an array defining HTTP methods, callback functions, permissions, and arguments
Click to reveal answer
intermediate
How do you ensure only authorized users can access your custom endpoint?
You add a permission_callback function in the endpoint args that returns true if the user has permission, or false otherwise. For example, checking if the user is logged in or has a specific capability.
Click to reveal answer
beginner
Where should you hook your custom endpoint registration code in WordPress?
You should hook it to the rest_api_init action. This ensures your endpoint is registered when the REST API initializes.
Click to reveal answer
Which hook is used to register custom REST API endpoints in WordPress?
Awp_loaded
Binit
Cadmin_init
Drest_api_init
✗ Incorrect
The rest_api_init hook is specifically designed for registering REST API routes.
What does the register_rest_route() function NOT require?
ANamespace
BRoute path
CDatabase connection
DCallback function
✗ Incorrect
You do not need to provide a database connection when registering a REST route.
How do you restrict access to a custom endpoint to logged-in users only?
ASet <code>methods</code> to POST only
BUse <code>permission_callback</code> to check if user is logged in
CAdd a nonce field in the URL
DUse <code>register_post_type</code>
✗ Incorrect
The permission_callback lets you control who can access the endpoint.
What HTTP methods can you specify in register_rest_route()?
AGET, POST, PUT, DELETE, PATCH
BOnly GET
COnly POST
DOnly PUT and DELETE
✗ Incorrect
You can specify any HTTP method supported by the REST API.
What is the purpose of the 'namespace' parameter in register_rest_route()?
ATo group related endpoints under a common path
BTo define the database table
CTo set user permissions
DTo specify the HTTP method
✗ Incorrect
Namespace helps organize endpoints logically, like 'myplugin/v1'.
Explain how to create a custom REST API endpoint in WordPress from start to finish.
Think about the steps to add a new URL path that returns data securely.
You got /4 concepts.
Describe how permission callbacks work in custom endpoint registration and why they are important.
Consider how you keep your API safe from unwanted users.
You got /4 concepts.
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?