Tarik Druskic
Tarik Druskic

Reputation: 77

Add a custom item above logout item in Woocommerce my account menu

I've been googling for a while now, but I can't seem to find an answer, nor anyone else with the same issue. I'm trying to add a menu item, "My Files", above the "Logout" item. Here's the code I'm using:

// Add Menu Item
function my_account_menu_items( $items ) {
    return array_merge(
        array_slice( $items, 0, count( $items ) - 1 ),
        array( 'my-files' => 'My Files' ),
        array_slice( $items, count( $items ) - 1 )
    );
}
add_filter( 'woocommerce_account_menu_items', 'my_account_menu_items', 10, 1);

I've tried calling the filter in a different way, without the last two arguments. I've also tried doing

function my_account_menu_items( $items ) {
    $items['my-files'] = 'My Files';
    return $items;
}

Upvotes: 2

Views: 888

Answers (3)

Dustin Markey
Dustin Markey

Reputation: 1

You could just make sure login stays at the bottom by unsetting it and adding it back last thing.

//keep logout the last menu item
function custom_my_account_menu_items( $items ) {
    unset($items['customer-logout']);
    $items['customer-logout'] = 'Logout';
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items', 999 );

Upvotes: 0

Ali_k
Ali_k

Reputation: 1661

This is tested and working:

function custom_my_account_menu_items( $items ) {

    $new_items = array();
    foreach($items as $key => $value){
        if($key != 'customer-logout'){
            $new_items[$key] = $value;
        }else{
            $new_items['my-files'] = 'My Files';
            $new_items[$key] = $value;
        }
    }
    return $new_items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254378

You should try the following instead:

add_filter( 'woocommerce_account_menu_items', 'add_item_to_my_account_menu', 30, 1 );
function add_item_to_my_account_menu( $items ) {
    $new_items = array();
    // Loop throu menu items
    foreach( $items as $key => $item ){
        if( 'customer-logout' == $key )
            $new_items['my-files'] = __( 'My Files' );

        $new_items[$key] = $item;
    }
    return $new_items;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Upvotes: 2

Related Questions