Using add_rewrite_endpoint to add custom permalink endpoints in WordPress
The WordPress add_rewrite_endpoint
function allows developers to add custom endpoints to the WordPress rewrite rules. An endpoint is a specific part of a URL that can trigger a custom behavior in the WordPress site. By adding an endpoint, developers can extend the functionality of WordPress URLs, making it possible to handle additional query variables and customize the way content is displayed.
When an endpoint is added, WordPress recognizes it as a valid part of the URL structure. This enables the creation of new URL patterns that can be used to trigger specific actions or display custom content. For instance, adding an endpoint to the URL structure can allow for the creation of custom pages or the addition of new query parameters that can be processed by WordPress.
The add_rewrite_endpoint
function integrates with the WordPress rewrite system, ensuring that the custom endpoints are registered and recognized during the URL parsing process. This integration allows developers to create more dynamic and flexible URL schemes within their WordPress installations.
Parameters
$name
(string), required. Name of the endpoint.$places
(int), required. Endpoint mask indicating where the endpoint should be added. Accepts a mask of:- EP_ALL
- EP_NONE
- EP_ALL_ARCHIVES
- EP_ATTACHMENT
- EP_AUTHORS
- EP_CATEGORIES
- EP_COMMENTS
- EP_DATE
- EP_DAY
- EP_MONTH
- EP_PAGES
- EP_PERMALINK
- EP_ROOT
- EP_SEARCH
- EP_TAGS
- EP_YEAR
$query_var
(string|bool), optional. Default is true. The name of the corresponding query variable. Set to false to skip registering a query variable for this endpoint. Defaults to the value of$name
.
Return Value
The function does not return a value.
Examples
How to Add a Custom Endpoint for User Profiles
function add_custom_user_profile_endpoint() {
add_rewrite_endpoint('profile', EP_ROOT);
}
add_action('init', 'add_custom_user_profile_endpoint');
This code snippet adds a custom endpoint profile
to WordPress URLs. When appended to the root URL, it will trigger the corresponding query variable. For example, visiting example.com/profile
would trigger this endpoint.
How to Add an Endpoint for Author Archives
function add_custom_author_archive_endpoint() {
add_rewrite_endpoint('bio', EP_AUTHORS);
}
add_action('init', 'add_custom_author_archive_endpoint');
This code snippet adds a custom endpoint bio
to author archive URLs. For example, visiting an author archive like example.com/author/username
can now be extended to example.com/author/username/bio
to trigger this endpoint.
Conclusion
The add_rewrite_endpoint
function in WordPress provides a robust mechanism for adding custom endpoints to your site’s URL structure. This functionality allows developers to append custom query variables to URLs, which can be particularly useful for creating custom pages, handling AJAX requests, or integrating with third-party APIs. By leveraging add_rewrite_endpoint
, developers can extend the capabilities of their WordPress sites, creating more dynamic and tailored user experiences. This function plays a key role in enhancing the flexibility and extensibility of WordPress, enabling the creation of more sophisticated and interactive web applications.