Adding custom WooCommerce account menu links to any desired URL

When working with WooCommerce in combination with other plugins that also have account-style areas on a site, it can make sense to choose one as the primary account area and link between them.

Here’s a snippet demonstrating how you can add custom links to a WooCommerce account menu and control the URL so the link takes the user to any URL you need. WooCommerce provides us with the necessary filters, we just need to implement this solution in two parts:

Part 1: Register the WooCommerce account menu item

First, we need to register the link ‘endpoint’ and link text with WooCommerce. This is then provided by the wc_get_account_menu_items() function when the account menu markup is generated. WooCommerce assumes we’re registering a custom account sub page or ‘endpoint’ here.

<?php
add_filter( 'woocommerce_account_menu_items', function ( $items, $endpoints ) {
$items['my-courses'] = 'My Courses';
return $items;
}, 10, 2 );

Part 2: Override the menu item’s URL

In order to change the link’s URL, we need to use an additional filter. This allows us to send the user any where we like such as some protected site content or, in this case, a courses archive.

<?php
add_filter( 'woocommerce_get_endpoint_url', function ( $url, $endpoint, $value, $permalink ) {
if ( $endpoint === 'my-courses' ) {
$url = home_url( 'my-courses' );
}
return $url;
}, 10, 4 );

About the author

Phil Kurth is a web developer living in Melbourne, Australia. Phil has a long history of WordPress development and enjoys building tools to empower others in their web design/development practice.

When not working with the web, Phil is usually spending time with his two young sons or is out hiking through the Australian bush.

Good dev stuff, delivered.

Product news, tips, and other cool things we make.

We never share your data. Read our Privacy Policy

© 2016 – 2024 Hookturn Digital. All rights reserved.