Hope everyone is fine, I am learning custom endpoints in WordPress. I know how to create endpoints and how they work(a little bit). But I got stuck when I wanted to add an endpoint to only one page. Let me give you an example of what I want to do.
- A page named as example having id 77.
- Set a custom template to it like
some-template.php
- If the user has set this template create endpoints like
example.com/example/endpoint1
orexample.com/example/endpoint2
- if the user opens another page like example.com/example-new/endpoint1, don't create an endpoint.
Here is the way I add endpoints.
add_action( 'init', 'add_new_e' );
add_filter( 'query_vars', 'filter_vars_e');
function add_new_e() {
add_rewrite_endpoint( 'mash', EP_PAGES );
}
function filter_vars_e($vars) {
$vars[] = 'mash';
return $vars;
}
I am flushing rewrite rules when the plugin activates/deactivates.
Now the problem is when I open another page with an endpoint, it doesn't throw a 404 error, and I know that I am using the EP_PAGES
mask.
Will someone help me with how I can achieve my goal.
Goal: add an endpoint to a specific page only having a specific template
Furthermore, I want to endpoints work in the same way as in woocommerce like my account page. Thanks in advance. If anybody needs other information please let me know.