To your theme’s WordPress menu, add a “My Account” link which links to a page that you designate. You can change the “My Account” text label into whatever you like. If the user is logged in, it will also have a sub-menu item that says “Log out”.
You can choose the position of the menu item on the menu. For example, you can make it be the second item from the left, or third item, or second from the right, or the last menu item on the right, etc.
You must also designate which menu to apply this to, since your theme may have more than 1 menu.
Edit lines 8 through 12.
On line 8, replace ‘main’ with the menu location of the menu that you want to modify.
On line 9, replace ‘9837’ with a unique id number. Use at least 4 digits, but more is okay.
On line 10, replace ‘9867’ with another unique id number.
Optionally, on line 11, replace ‘My Account’ with your desired text for the menu item.
Optionally, on line 12, replace get_bloginfo('url') . '/my-account/'
with the URL for the page that you want the menu item to link to.
Optionally, edit line 64 if you want to choose the position of the menu item (see examples on lines 50 — 62). By default, this will place the new “My Account” link at the end of the menu. To change the position, replace line 64 with one of the examples from lines 50 — 62.
You can pass custom CSS classes to the menu items on lines 28 and 44. Line 28 is for the “My Account” menu item. Line 44 is for the “Log out” sub-menu item.
/** * Add My Account Link only on the header menu, * and if user is logged in, add log out link as sub-menu item */ function isa_add_menu_login_out_link( $items, $args ) { $theme_location = 'main'; // REPLACE 'main' with the menu location of the menu that you want to modify $login_menu_item_db_id = 9837; // REPLACE 9837 with a unique id number $logout_menu_item_db_id = 9867; // REPLACE 9867 with a unique id number $label = 'My Account'; // REPLACE 'My Account' with your desired text for the menu item. $account_url = get_bloginfo('url') . '/my-account/'; // REPLACE with the URL for the "My Account" page if ( $theme_location !== $args->theme_location ) return $items; $new_links = array(); // Create a My Account link $item = array( 'title' => $label, 'menu_item_parent' => 0, 'ID' => 'my-account', 'db_id' => $login_menu_item_db_id, // a unique id # 'url' => get_bloginfo('url') . '/my-account', // 'classes' => array( 'custom-menu-item' )// optional ); $new_links[] = (object) $item; // Add the new menu item to our array unset( $item ); // only if user is logged-in, do "log out" link if ( is_user_logged_in() ) { $item = array( 'title' => 'Log out', 'menu_item_parent' => $login_menu_item_db_id, // the 'db_id' of the "My Account" item 'ID' => 'log-out', 'db_id' => $logout_menu_item_db_id, // a unique id # 'url' => wp_logout_url(get_permalink()), // 'classes' => array( 'custom-menu-item' )// optional ); $new_links[] = (object) $item; // Add the new menu item to our array } // SET $index equal to the order number where you want the menu item to appear on the menu // // Examples: // // $index = 1 /* this would make it the first menu item from left */ // // $index = 2 /* this would make it the second menu item from left */ // // $index = count( $items ) - 2 /* this would insert it before the last 2 items, making it the 3rd item from right */ // // $index = count( $items ) - 1 /* this would insert it before the last item, making it the 2nd item from right */ // // $index = count( $items ) /* this would make it the last item */ $index = count( $items ); // integer, the order number. // Insert the new links at the appropriate place. array_splice( $items, $index, 0, $new_links ); return $items; } add_filter( 'wp_nav_menu_objects', 'isa_add_menu_login_out_link', 10, 2 );
Justin
January 5th, 2017 at 1:34 pm
Hi Isabel,
This almost perfect for what I need. I would like to add a second submenu item to My Account though. How would I go about doing that please? Thanks in advance!!