I would like to know how can i rearrange the menu tabs on My-Account Page in WooCommerce.
I have added a new menu called "Affiliate Dashboard" using the following code below but i want to show it before the "Log Out" menu so that it appears in between "Account Details" & "Log Out" menus.
So the arrangement will be like this.
1- Dashboard
2- Orders
3- Coupons
4- Addresses
5- Account Details
6- Affiliate Dashboard
7- Log out
Please see screenshot.
//First hook that adds the menu item to my-account WooCommerce menu
function affiliate_home_link( $menu_links ){
// we will hook "womanide-forum" later
$new = array( 'affiliate-home' => 'Affiliate Dashboard' );
// or in case you need 2 links
// $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );
// array_slice() is good when you want to add an element between the other ones
$menu_links = array_slice( $menu_links, 0, 5, true )
+ $new
+ array_slice( $menu_links, 5, NULL, true );
return $menu_links;
}
add_filter ( 'woocommerce_account_menu_items', 'affiliate_home_link' );
// Second Filter to Redirect the WooCommerce endpoint to custom URL
function affiliate_home_hook_endpoint( $url, $endpoint, $value, $permalink ){
if( $endpoint === 'example-forum' ) {
// This is where you add the custom URL, it could be external like, in this case, we need to go to my profile on the bbpress froum
// I will use this function (bp_core_get_username( bp_loggedin_user_id() );) to get my profile user id and add it to the URL as shown below
$url = site_url() .'/affiliate-home/' . bp_core_get_username( bp_loggedin_user_id() );
}
return $url;
}
add_filter( 'woocommerce_get_endpoint_url', 'forum_example_hook_endpoint', 10, 4 );
Any help will be appreciated.
Thanks!
2
Answers
You can rearrange your $menu_items in the same function like so:
Tested working: https://i.imgur.com/DM1eMWH.png
You can use a custom filter and set priorities to each endpoint with a default priority of zero for each. This was my approach when I needed to achieve tweak the menu for a number of custom items.