adding Login/Log out to menu with redirects

Hi guys,

Hope you guys are doing well. I'm trying to add dynamic login/logout to menu but want the login to go to our woocommerce account page and logout to go back to home.

This is the base code I've been playing with in functions.php (I know it's not correct at the moment). I was hoping you could fill in the blanks though. I'm sure this would help other out as well :slight_smile:

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 == 'primary') {

$items .= '

  • Log Out
  • ';

    }

    elseif (!is_user_logged_in() && $args->theme_location == 'primary') {

    $items .= '

  • Log In
  • ';

    }

    return $items;

    }

    Thank you!

    • Derek
      • Site Builder, Child of Zeus

      `
      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 == 'primary') {

      $items .= '

      <li><a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>
      ';

      }

      elseif (!is_user_logged_in() && $args->theme_location == 'primary') {

      $items .= '

      <li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log In</a></li>
      ';

      }

      return $items;

      }
      '

    • Adam Czajczyk
      • Support Gorilla

      Hello Derek,

      I hope you're well today and thank you for your question!

      I think this code should work for you better:

      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 == 'primary') {
      
      	$items .= '<a href="' . wp_logout_url( home_url() ) .'">Log Out</a>'; //logout
      }
      
      elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
      
      	$items .= '<a href="' . wp_login_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) . '">Log In</a>'; //login
      }
      
      return $items;
      
      }

      The "log out" link would redirect users to the homepage of your site while "login url" would first direct them to standard WP login form and after they login they'd be automatically taken to "my account" WC page.

      Let me know if that helped, please!

      Best regards,
      Adam

    • Derek
      • Site Builder, Child of Zeus

      Sorry, just to clarify, we have 'registration enabled' on the woocommerce account page - so basically just another nice looking sign up form. It would be ideal to be directed there on login. Thanks for looking into this for me!
      Derek

    • Predrag Dubajic
      • Support

      Hey Derek,

      If we look at this part of code:
      wp_login_url( get_permalink( wc_get_page_id( 'myaccount' ) ) )
      It links to login page that has redirect ready for my-account page, if you need to redirect Login menu item to custom page you can replace that code with an URL of your desired page, for example replace this:
      $items .= '<a href="' . wp_login_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) . '">Log In</a>'; //login
      With this:
      $items .= '<a href="http://mysite.com/mypage">Log In</a>'; //login

      Is that what you're looking for?

      Best regards,
      Predrag

    • Derek
      • Site Builder, Child of Zeus

      Thanks Predrag! that works great! It's exactly what we needed.

      This works when we paste into theme functions.php, but when we try and add it to another site in the child functions.php it does not display (we removed caching, flushed cash, etc.)

      I think it's because the menu name is different under Appearance>Menus. It's listed as 'Main menu' so this code doesn't work:
      if (is_user_logged_in() && $args->theme_location == 'primary') {

      tried to replace with 'Main menu', 'Main', etc. and doesn't work.
      Any ideas?
      Thanks man, Derek

    • Predrag Dubajic
      • Support

      Hi Derek,

      Those other sites are not using the same parent theme, is that correct?

      Above code, as you notices, catches on primary menu, however you need to find actual define of the menu instead of using name that is shown in your wp-admin.

      For example, 2016 theme has this in functions.php that defines menus:

      register_nav_menus( array(
      	'primary' => __( 'Primary Menu', 'twentysixteen' ),
      	'social'  => __( 'Social Links Menu', 'twentysixteen' ),
      ) );

      This is where the "primary" part comes from.
      So what you could try is doing a search through your theme files and try finding register_nav_menus, you should see similar code as above and get correct menu name from there that you can use in your code.

      Hope this makes sense :slight_smile:

      Best regards,
      Predrag

    Thank NAME, for their help.

    Let NAME know exactly why they deserved these points.

    Gift a custom amount of points.