Last active
December 16, 2024 16:27
-
-
Save neilgee/075a1e75cb5b5186748f to your computer and use it in GitHub Desktop.
Add WooCommerce MyAccount Login/Logout Menu Items to Registered Menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php //<~ don't add me in | |
| add_filter( 'wp_nav_menu_items', 'my_account_loginout_link', 10, 2 ); | |
| /** | |
| * Add WooCommerce My Account Login/Logout to Registered Menu | |
| * | |
| * @link https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area | |
| */ | |
| function my_account_loginout_link( $items, $args ) { | |
| if (is_user_logged_in() && $args->theme_location == 'primary') { //change your theme registered menu name to suit | |
| $items .= '<li><a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">Log Out</a></li>'; //change logout link, here it goes to 'shop', you may want to put it to 'myaccount' | |
| } | |
| elseif (!is_user_logged_in() && $args->theme_location == 'primary') {//change your theme registered menu name to suit | |
| $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log In</a></li>'; | |
| } | |
| return $items; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works very well for me.