0
0
Wordpressframework~5 mins

Custom endpoint registration in Wordpress

Choose your learning style9 modes available
Introduction

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.

You want to add a new page with a unique URL that shows custom data.
You need to create a special URL to handle form submissions or API calls.
You want to add extra sections to user profiles or shop pages.
You want to create SEO-friendly URLs for custom content types.
You want to extend WooCommerce or other plugins with new URL paths.
Syntax
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.

Examples
This adds a /news/ endpoint at the site root, like example.com/news/.
Wordpress
function add_news_endpoint() {
    add_rewrite_endpoint( 'news', EP_ROOT );
}
add_action( 'init', 'add_news_endpoint' );
This adds a /profile/ endpoint to pages, so example.com/about/profile/ works.
Wordpress
function add_profile_endpoint() {
    add_rewrite_endpoint( 'profile', EP_PAGES );
}
add_action( 'init', 'add_profile_endpoint' );
Sample Program

This code adds a new URL /special/ that shows a custom message. It also flushes rewrite rules on activation so the URL works immediately.

Wordpress
<?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' );
OutputSuccess
Important Notes

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.

Summary

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.