-
AuthorPosts
-
November 7, 2015 at 5:40 am #531722
Hi guys,
I’m trying to add login / log out links to my top menu in my Enfold theme and WooCommerce powered website. I’m using the following code in my functions.php file as per the recommendations by Woothemes:
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'top-menu') { $items .= '<li><a href="https://www.mywebsite.com/account/customer-logout/">Log Out</a></li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'top-menu') { $items .= '<li><a href="https://www.mywebsite.com/account/customer-login/">Log In</a></li>'; } return $items; }
Original article here: https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area
However nothing I try seems to work. How must I adjust the above code to make it work with the Enfold theme?
Note: the reason I need to do it via the functions file is because I need to use specific login and logout pages other than the WP defaults.
Thank you.
November 9, 2015 at 3:29 pm #532637Hi there, can anyone help me with this? I see all posts before and after mine have been answered except mine.
Thank you.
November 10, 2015 at 4:50 pm #533437Hey!
I didn’t test your code out but just from looking at it you’ll need to change the “top-menu” part to point to our theme location names.
You can change it to this to target the primary menu.
if (is_user_logged_in() && $args->theme_location == 'avia') {
Or switch it to “avia2” or “avia3” to target the header / footer menu areas.
Best regards,
ElliottNovember 10, 2015 at 5:40 pm #533492Hi Elliott,
Thanks for getting back to me. This is exactly what I was missing! Works perfectly now. Thank you.
In case anyone else is trying to add sign in/sign out logic to their header that uses non WP default pages (like with a WooCommerce store) via the functions.php file, here’s the code:
// Add Log in / log out links to top menu add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'avia2') { $items .= '<li><a href="your-domain/account/customer-logout/">Log Out</a></li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'avia2') { $items .= '<li><a href="your-domain/account/">Log In</a></li>'; } return $items; }
This will add the links to the top menu in your theme. Just replace the URL’s with your own URL’s to your log in and log out pages.
November 13, 2015 at 2:52 pm #535402Hey!
glad it’s working for you and thanks a lot for sharing!
Cheers!
Andy -
AuthorPosts
- The topic ‘Add Login / Log out links to top menu’ is closed to new replies.