-2

I'm using this code to add a new menu item to WooCommerce my account page menu. Is there a way to open my url in a new tab? (like target="_blank")

add_filter(
  "woocommerce_account_menu_items",
  function ($items, $endpoints) {
    $items["my-item"] = "License hub";

    return $items;
  },
  10,
  2,
);

add_filter(
  "woocommerce_get_endpoint_url",
  function ($url, $endpoint, $value, $permalink) {
    if ($endpoint === "my-item") {
      $url = "https://my-domain.net/";
    }
    return $url;
  },
  10,
  4,
);

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve this by adding a custom class to your menu item and then using JavaScript to set the target="_blank" attribute.

// Add custom class to the menu item add_filter( 'woocommerce_account_menu_item_classes', function( $classes, $endpoint ) {
    if ( $endpoint === 'my-item' ) {
        $classes[] = 'external-link';
    }
    return $classes;
}, 10, 2 );
// Add target="_blank" for the custom menu item add_action( 'wp_footer', function() {
    if ( is_account_page() ) {
        ?>        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $('.external-link a').attr('target', '_blank');
            });
        </script>
        <?php    }
});

2 Comments

Really no hook or settings for this?
Nope, no hook available for this.
0

You can add this to your theme's functions.php, it will target the custom menu item's link, then add the target="_blank".

add_action( 'wp_footer', function() {
    if ( is_account_page() ) {
        ?>
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                const link = document.querySelector('a[href="https://my-domain.net/"]');
                if (link) {
                    link.setAttribute('target', '_blank');
                    link.setAttribute('rel', 'noopener noreferrer');
                }
            });
        </script>
        <?php
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.