Reputation: 312
I’m trying to add custom template account-details.php
to my new endpoint in my-account area.
I have added the new account-details endpoint first:
add_action( 'init', 'co_add_my_account_endpoint' );
function co_add_my_account_endpoint() {
add_rewrite_endpoint( 'account-details', EP_ROOT | EP_PAGES );
}
and here I’m adding the custom template:
add_filter( 'wc_get_template', 'co_custom_endpoint', 10, 5 );
/**
* Add account details custom template
*
* @param $located
* @param $template_name
* @param $args
* @param $template_path
* @param $default_path
* @since 2.0
* @return string $located
*/
function co_custom_endpoint($located, $template_name, $args, $template_path, $default_path) {
global $wp;
if( 'myaccount/my-account.php' == $template_name ) {
$located = wc_locate_template( 'myaccount/account-details.php', $template_path, JGTB_PATH . 'templates/' );
}
return $located;
}
At the end I flush rewrite rules manually, but my template is still not loading on frontend. Anyone can see what am I doing wrong? I have found other posts on stack-overflow regarding this, but if I replicate exactly the same it doesn’t work for me either.. any ideas?
Any help is very appreciated!
Upvotes: 0
Views: 626
Reputation: 1465
use below code to your active theme's function.php
.
function custom_account_details_page_endpoints() {
add_rewrite_endpoint( 'account-details', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'custom_account_details_page_endpoints' );
function custom_account_details_query_vars( $vars ) {
$vars[] = 'account-details';
return $vars;
}
add_filter( 'query_vars', 'custom_account_details_query_vars', 0 );
function custom_account_details_query_vars_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'custom_account_details_query_vars_flush_rewrite_rules' );
Be sure to place the account-details.php
file in the myaccount
folder.
function custom_account_details_endpoint_content() {
include 'woocommerce/myaccount/account-details.php';
}
add_action( 'woocommerce_account_account-details_endpoint', 'custom_account_details_endpoint_content' );
By doing this make sure to update permalinks by going to Dashboard -> settings -> permalinks and clik on save settings
Upvotes: 1