Custom endpoints let you add new URLs to your WordPress site that show special content or handle custom actions. This helps you create unique pages or features without changing core files.
Custom endpoint registration in Wordpress
function add_custom_endpoint() {
add_rewrite_endpoint( 'endpoint-name', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_custom_endpoint' );add_rewrite_endpoint adds a new URL part WordPress recognizes.
Use EP_ROOT for site root URLs or EP_PAGES for pages.
/news/ endpoint at the site root, like example.com/news/.function add_news_endpoint() {
add_rewrite_endpoint( 'news', EP_ROOT );
}
add_action( 'init', 'add_news_endpoint' );/profile/ endpoint to pages, so example.com/about/profile/ works.function add_profile_endpoint() {
add_rewrite_endpoint( 'profile', EP_PAGES );
}
add_action( 'init', 'add_profile_endpoint' );This code adds a new URL /special/ that shows a custom message. It also flushes rewrite rules on activation so the URL works immediately.
<?php // Register a custom endpoint 'special' at site root function register_special_endpoint() { add_rewrite_endpoint( 'special', EP_ROOT ); } add_action( 'init', 'register_special_endpoint' ); // Display content when visiting the endpoint function special_endpoint_content() { global $wp_query; if ( isset( $wp_query->query_vars['special'] ) ) { echo '<h1>Welcome to the Special Page!</h1>'; exit; // Stop normal page load } } add_action( 'template_redirect', 'special_endpoint_content' ); // Flush rewrite rules on plugin/theme activation function flush_rewrite_on_activation() { register_special_endpoint(); flush_rewrite_rules(); } register_activation_hook( __FILE__, 'flush_rewrite_on_activation' );
After adding a new endpoint, you must flush rewrite rules by visiting Settings > Permalinks or calling flush_rewrite_rules().
Use template_redirect hook to show content for your endpoint before WordPress loads the normal page.
Endpoints add query variables you can check with $wp_query->query_vars.
Custom endpoints add new URL parts to your WordPress site for special content or actions.
Use add_rewrite_endpoint inside an init hook to register them.
Flush rewrite rules after adding endpoints to make URLs work.
Practice
add_rewrite_endpoint in WordPress?Solution
Step 1: Understand the function purpose
add_rewrite_endpointis 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 CQuick Check:
add_rewrite_endpoint adds URL parts = D [OK]
- Confusing endpoints with plugin registration
- Thinking it creates database tables
- Assuming it loads scripts or styles
add_rewrite_endpoint when registering a custom endpoint?Solution
Step 1: Recall WordPress hook usage
Theinithook runs early and is the recommended place to add rewrite rules and endpoints.Step 2: Eliminate incorrect hooks
wp_enqueue_scriptsis for scripts/styles,admin_menuis for admin menus, andwp_footerruns late in page rendering.Final Answer:
init -> Option DQuick Check:
add_rewrite_endpoint called on init hook = A [OK]
- Using wp_enqueue_scripts instead of init
- Adding endpoints in admin_menu hook
- Placing code in wp_footer hook
add_action('init', function() {
add_rewrite_endpoint('special', EP_PERMALINK | EP_PAGES);
});Solution
Step 1: Analyze add_rewrite_endpoint parameters
The first parameter 'special' is the endpoint name; the second parameter sets where it applies: permalinks and pages.Step 2: Understand the effect on URLs
This adds '/special/' to URLs of posts and pages, allowing custom handling of that URL part.Final Answer:
Adds '/special/' as an endpoint to permalinks and pages URLs -> Option AQuick Check:
Endpoint 'special' added to permalinks/pages = B [OK]
- Thinking it removes endpoints
- Confusing endpoints with admin menus
- Assuming rewrite rules flush automatically
add_action('init', function() {
add_rewrite_endpoint('promo', EP_ROOT);
});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 BQuick Check:
Flush rewrite rules after adding endpoints = C [OK]
- Skipping rewrite rules flush
- Using wrong hook instead of init
- Thinking endpoint names must be special
Solution
Step 1: Register endpoint on init hook
Functionadd_download_endpointadds the endpoint on 'init' hook, which is correct.Step 2: Flush rewrite rules only once on activation
Usingregister_activation_hookto call the endpoint function and then flush rewrite rules ensures flushing happens once on plugin activation.Step 3: Evaluate other options
add_action('init', function() { add_rewrite_endpoint('download', EP_ALL); flush_rewrite_rules(); });flushes rules on every page load (bad).function add_download_endpoint() { add_rewrite_endpoint('download', EP_ROOT); flush_rewrite_rules(); } add_action('init', 'add_download_endpoint');flushes inside init (bad).register_activation_hook(__FILE__, function() { add_rewrite_endpoint('download', EP_PERMALINK | EP_PAGES); });does not flush rules at all.Final Answer:
Register endpoint on 'init' hook and flush rewrite rules once on plugin activation -> Option AQuick Check:
Register on init + flush once on activation = A [OK]
- Flushing rewrite rules on every page load
- Not flushing rewrite rules at all
- Registering endpoint outside init hook
Ready to prove it?
Pass the test and this pattern moves unknown
