I found this solution by LoicTheAztec to be solid for WooCommerce-native code. It outlines the core method for adding a custom menu item to the My Account page and override its link. So my answer here builds on his solution by adding a bit more clarity, context, and practical notes.
In your code:
You are using the correct filter-hook (woocommerce_account_menu_items) to add menu item to "My Account" page
However, you can't use link attribute like target="_blank" directly via the woocommerce_get_endpoint_url filter, since it only controls the URL, and not the HTML output of the link. So you need to add JavaScript/jQuery after the page is rendered
First, you need to add your custom item to the menu in "My Account" page:
You can do this by using a translatable label, which supports localization (translation):
// Add custom menu item to "My Account" page
add_filter('woocommerce_account_menu_items', 'add_my_account_custom_menu_item');
function add_my_account_custom_menu_item( $menu_items ) {
$menu_item_key = 'custom_link'; // your custom menu item key
$menu_items[$menu_item_key] = __('License hub', 'woocommerce'); // Your custom, translatable label
return $menu_items;
}
Add link and make it open in a new tab:
Since you are doing a redirect, you can use the template_redirect hook to inject JavaScript, and set the target="_blank" attribute:
// Add custom link to custom menu item and open in new tab
add_action('template_redirect', 'open_custom_menu_link_in_new_tab', 10);
function open_custom_menu_link_in_new_tab() {
// Check if user is logged in and is on "my account" page
if ( is_user_logged_in() && is_account_page() ) {
$menu_item_key = 'custom_link'; // Your custom menu item key
$custom_link = esc_url('https://my-domain.net/'); // Your custom link
// jQuery code
wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}').attr('target','_blank');");
}
}
Notes:
woocommerce-MyAccount-navigation-link: Default class for all menu item in WooCommerce My Account page.
woocommerce-MyAccount-navigation-link--{$menu_item_key}: Specific class for your custom item, based on the key you set.
This solution will open a new tab with your custom link. It does not load any WooCommerce tab content, unlike the default menu items which are tied to internal endpoints.
This solution is tested and works with WooCommerce Legacy templates.