admin管理员组文章数量:1418660
I want to add a sub endpoint to custom endpoint defined for my woocommerce myaccount template. For example :
custom endpoint - my-account/alumni
subendpoint - my-account/alumni/index
I have added the functionality to add the custom endpoint but when i am trying to add the sub-endpoint it is redirecting me to my-account/ and not loading the template
I have already tried refreshing the permalinks. and changing the position of endpoint and sub-endpoint definitions. I have written this code in functions.php
add_action( 'init', 'add_alumni_index_account_endpoint' );
function add_alumni_index_account_endpoint() {
add_rewrite_endpoint( 'alumni-portal/alumni-index', EP_PAGES );
}
add_action( 'woocommerce_account_alumni-index_endpoint', 'alumni_index_account_endpoint_content' );
function alumni_index_account_endpoint_content() {
//if ( current_user_can('administrator') ) {
wc_get_template( 'myaccount/alumni-index.php' );
//}
}
add_action( 'init', 'add_alumni_portal_account_endpoint' );
function add_alumni_portal_account_endpoint() {
add_rewrite_endpoint( 'alumni-portal', EP_PAGES );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
//if ( current_user_can('administrator') ) {
$menu_links = array_slice( $menu_links, 0,3 , true )
+ array( 'alumni-portal' => __('Alumni Portal') )
+ array_slice( $menu_links, 3, NULL, true );
//}
return $menu_links;
}
add_action( 'woocommerce_account_alumni-portal_endpoint', 'alumni_portal_account_endpoint_content' );
function alumni_portal_account_endpoint_content() {
//if ( current_user_can('administrator') ) {
wc_get_template( 'myaccount/alumni-portal.php' );
//}
}
add_filter("woocommerce_get_query_vars", function ($vars) {
foreach (["alumni-portal/alumni-index","alumni-portal"] as $e) {
$vars[$e] = $e;
}
return $vars;
});
Result should be that my sub-endpoint should load its respective template rather than redirecting to my-account.
alumni-index.php has a simple code of hello
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<p>hello</p>
I want to add a sub endpoint to custom endpoint defined for my woocommerce myaccount template. For example :
custom endpoint - my-account/alumni
subendpoint - my-account/alumni/index
I have added the functionality to add the custom endpoint but when i am trying to add the sub-endpoint it is redirecting me to my-account/ and not loading the template
I have already tried refreshing the permalinks. and changing the position of endpoint and sub-endpoint definitions. I have written this code in functions.php
add_action( 'init', 'add_alumni_index_account_endpoint' );
function add_alumni_index_account_endpoint() {
add_rewrite_endpoint( 'alumni-portal/alumni-index', EP_PAGES );
}
add_action( 'woocommerce_account_alumni-index_endpoint', 'alumni_index_account_endpoint_content' );
function alumni_index_account_endpoint_content() {
//if ( current_user_can('administrator') ) {
wc_get_template( 'myaccount/alumni-index.php' );
//}
}
add_action( 'init', 'add_alumni_portal_account_endpoint' );
function add_alumni_portal_account_endpoint() {
add_rewrite_endpoint( 'alumni-portal', EP_PAGES );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
//if ( current_user_can('administrator') ) {
$menu_links = array_slice( $menu_links, 0,3 , true )
+ array( 'alumni-portal' => __('Alumni Portal') )
+ array_slice( $menu_links, 3, NULL, true );
//}
return $menu_links;
}
add_action( 'woocommerce_account_alumni-portal_endpoint', 'alumni_portal_account_endpoint_content' );
function alumni_portal_account_endpoint_content() {
//if ( current_user_can('administrator') ) {
wc_get_template( 'myaccount/alumni-portal.php' );
//}
}
add_filter("woocommerce_get_query_vars", function ($vars) {
foreach (["alumni-portal/alumni-index","alumni-portal"] as $e) {
$vars[$e] = $e;
}
return $vars;
});
Result should be that my sub-endpoint should load its respective template rather than redirecting to my-account.
alumni-index.php has a simple code of hello
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<p>hello</p>
Share
Improve this question
asked Jul 26, 2019 at 6:23
user172501user172501
1 Answer
Reset to default 1You can't add alumni-portal/alumni-index
as a separate endpoint, because this clashes with the alumni-portal
endpoint. This is because rewrite endpoints will capture anything after /
as their value. So alumni-portal/alumni-index
is the same as alumni-portal
, but the alumni-portal
query var is set to alumni-index
.
So rather than registering both endpoints, register alumni-portal
a single endpoint, and then check for which value for alumni-portal
is passed. If the value is index
, then display the appropriate template.
So register your endpoint with the woocommerce_get_query_vars
filter:
add_filter(
'woocommerce_get_query_vars',
function( $query_vars ) {
$query_vars['alumni'] = 'alumni';
return $query_vars;
}
);
Then use the woocommerce_account_alumni_endpoint
action to display the page. These account endpoint hooks will pass the "value" of the endpoint as an argument to the callback function. So if I visit my-account/alumni/index
the value passed will be index
, and you can use this to display the appropriate template:
add_action(
'woocommerce_account_alumni_endpoint',
function( $value ) {
if ( $value === 'index' ) {
wc_get_template( 'myaccount/alumni-index.php' );
} else {
wc_get_template( 'myaccount/alumni-portal.php' );
}
}
);
If you're filtering the account page title you don't get the value passed, but since you do get the endpoint you can get this with get_query_var()
:
add_filter(
'woocommerce_endpoint_alumni_title',
function( $title, $endpoint ){
$value = get_query_var( $endpoint );
if ( $value === 'index' ) {
$title = 'Alumni Index';
} else {
$title = 'Alumni Portal';
}
return $title;
},
10,
2
);
本文标签: add sub subpage endpoint to woocommerce plugin myaccount section
版权声明:本文标题:add sub subpage endpoint to woocommerce plugin my-account section 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745293706a2651951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论