How do I add a custom endpoint to the home page in WordPress?
-
In WordPress it’s possible to add a
custom endpoint
to pages, such as:https://example.com/example/test
wheretest
is the custom endpoint.function myEndpoint_init() { $myEndpoint = "test"; add_rewrite_endpoint( $myEndpoint , EP_ALL ); } add_action( 'init', 'myEndpoint_init',11);
If you then hook into
template_redirect
you can detect if there’s an endpoint and do something custom accordingly… such as:function myCustomCode() { //do stuff echo "the page id: " . get_the_ID(); } add_action('template_redirect', 'myCustomCode');
This works great on every page, except for the home page, which doesn’t work as expected.
Example output for
https://example.com/example/test
: the page id: 57 (this is correct)For
https://example.com/example/
: the page id: 57 (this is correct)For
https://example.com/
: the page id: 5 (this is correct)For
https://example.com/test
the page id: 1 (this is not correct)And it also does not print the correct page.
What am I missing? How would I achieve this?
- The topic ‘How do I add a custom endpoint to the home page in WordPress?’ is closed to new replies.