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.