admin管理员组文章数量:1320655
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/example/endpoint1
orexample/example/endpoint2
- if the user opens another page like example/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.
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/example/endpoint1
orexample/example/endpoint2
- if the user opens another page like example/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.
Share Improve this question edited Oct 13, 2020 at 8:34 Raashid Din asked Oct 13, 2020 at 8:21 Raashid DinRaashid Din 2182 silver badges19 bronze badges1 Answer
Reset to default 2No, add_rewrite_endpoint()
doesn't limit to any specific page, only add_rewrite_rule()
can do that.
However, if I understand it correctly, you can use the pre_handle_404
hook to check if the endpoint query is set and that the page uses a specific template, then throw a 404 error if those conditions are not met.
Working example based on your code:
add_filter( 'pre_handle_404', 'wpse_376370', 10, 2 );
function wpse_376370( $value, $wp_query ) {
if (
// It's a valid "mash" endpoint request,
$wp_query->get( 'mash' ) &&
// but the request is not a Page or its slug is not 'example',
! $wp_query->is_page( 'example' ) &&
// and the Page is not using the template some-template.php.
! is_page_template( 'some-template.php' )
) {
// Therefore, we throw a 404 error
$wp_query->set_404();
// and avoid redirect to the page. (at example/example)
remove_action( 'template_redirect', 'redirect_canonical' );
}
return $value;
}
本文标签: pluginsIs there a way to add custom endpoint to specific page only
版权声明:本文标题:plugins - Is there a way to add custom endpoint to specific page only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742065033a2418783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论