Login function on primary menu

I’ve gotten pretty far on this, but there are 2 niggles I can’t figure out:

  1. When a user is not logged in, just show the “Log In” item and no dropdown under the community menu. Right now I have another Log In, in there, but even if I remove it, I end up with a blank drop down.

  2. When a user is logged in, the Log In disappears from the main menu completely, and the Log Out appears along with the other items under the Community sub-menu.

    // Custom Menus
    /**

    • Community */
      if ( ! function_exists( ‘x_bbpress_navbar_menu’ ) ) :
      function x_bbpress_navbar_menu( $items, $args ) {

    if ( X_BBPRESS_IS_ACTIVE && x_get_option( ‘x_bbpress_header_menu_enable’ ) == ‘1’ ) {

    $submenu_items = ‘’;

    if ( is_user_logged_in() ) {
    $submenu_items .= ’

  3. ’ . __( ‘Members’, ‘x’ ) . ‘
  4. ’;
    $submenu_items .= ’
  5. ’ . __( ‘Favorites’, ‘x’ ) . ‘
  6. ’;
    $submenu_items .= ’
  7. ’ . __( ‘Subscriptions’, ‘x’ ) . ‘
  8. ’;
    $submenu_items .= ’
  9. ’ . __( ‘Downloads’, ‘x’ ) . ‘
  10. ’;
    }

    if ( ! X_BUDDYPRESS_IS_ACTIVE || X_BUDDYPRESS_IS_ACTIVE && x_get_option( ‘x_buddypress_header_menu_enable’ ) == ‘’ ) {
    if ( ! is_user_logged_in() ) {
    $submenu_items .= ’

  11. ’ . __( ‘Log in’, ‘x’ ) . ‘
  12. ’;
    } else {
    $submenu_items .= ’
  13. ’ . __( ‘Profile’, ‘x’ ) . ‘
  14. ’;
             $submenu_items .= '<li class="menu-item menu-item-bbpress-navigation"><a href="/my-account" class="cf"><i class="x-icon-cog" data-x-icon="&#xf007;" aria-hidden="true"></i> <span>' . __( 'My Account', '__x__' ) . '</span></a></li>';
             $submenu_items .= '<li class="menu-item menu-item-bbpress-navigation"><a href="' . wp_logout_url() . '" class="cf"><i class="x-icon-cog" data-x-icon="&#xf08b;" aria-hidden="true"></i> <span>' . __( 'Log Out', '__x__' ) . '</span></a></li>';
     }
    

    }

    if ( $args->theme_location == ‘primary’ ) {
    $items .= ‘

  15. ’
    . ‘’
    . ’ ’ . __( ‘Community’, ‘x’ ) . ‘’
    . ‘
    ’
    . ‘
      ’
      . $submenu_items
      . ‘
    ’
    . ‘
  16. ’;
    }
    }

    return $items;

    }

    function autov_add_loginout_navitem( $items, $args ) {
    if( $args->theme_location == ‘primary’ ){
    $login_item = ‘

  17. ’.wp_loginout($_SERVER[‘REQUEST_URI’], false).’
  18. ’;
    $items .= $login_item;
    }
    return $items;
    }

    add_filter( ‘wp_nav_menu_items’, ‘x_bbpress_navbar_menu’, 9996, 2 );
    add_filter( ‘wp_nav_menu_items’, ‘autov_add_loginout_navitem’, 9997, 2 );
    endif;

Hello There,

Thanks for writing in!

1.) There is a need to add a condition in your code so that when the user needs to log in, there is no sub menu. So please update your code like this:

  $submenu = '';

  if ( $submenu_items != '' ) {
    $submenu = . '<ul class="sub-menu">'
                . $submenu_items
              . '</ul>';
  }


  if ( $args->theme_location == 'primary' ) {
    $items .= '<li class="menu-item current-menu-parent menu-item-has-children x-menu-item x-menu-item-bbpress">'
              . '<a href="/forums" class="x-btn-navbar-bbpress">'
                . '<span><i class="x-icon-comment" data-x-icon="&#xf075;" aria-hidden="true"></i><span class="x-show-desktop"> ' . __( 'Community', '__x__' ) . '</span></span>'
              . '</a>'
              . $submenu
            . '</li>';
  }

2.) The best way you can do this is to use a guide in your condition. having this some sort of guide or flow chart, you can add/remove menu items or sub menu item.


if user is login 
   display menu item 
   (or display nothing)
else 
   display login button


if user is not login 
   display sub menu login button
else 
   display sub menu item 1
   display sub menu item 2
   display sub menu item 3
   display sub menu item 4
   display logout sub menu item

Hope this will help.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.