Site logo

[Resolved] Dynamic login/logout item in menu

Home Forums Support [Resolved] Dynamic login/logout item in menu

Home Forums Support Dynamic login/logout item in menu

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1874268
    mkjj

    I would like to add a dynamic login/logout item to the main menu. Users that are logged-out should see “login”, logged-in users should see “logout”. The logout item has a submenu. This can easily be done with a standard WP filter:

    function add_login_logout_register_menu( $items, $args ) {
      $login = home_url( '/login/' );
      $logout = home_url( '/login/' );
      if ( $args->theme_location == 68 ) {
        return $items;
      }
      if ( is_user_logged_in() ) {
        $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><a href="' . $logout . '">' . __( 'Logout' ) . '</a>
        <ul class="sub-menu">
        <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="https://doriskirch.de/meine-kurse/">Meine Kurse</a></li>
        </ul></li>';
      }
      else {
        $items .= '<li><a href="' . $login . '">' . __( 'Login' ) . '</a></li>';
      }
      return $items;
    } 
    add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 199, 2 );

    The only problem is that I don’t see the dropdown arrow that opens the subitem.

    The standard menu uses a SVG like so:

    <span role="presentation" class="dropdown-menu-toggle">
    <span class="gp-icon icon-arrow">
    <svg viewBox="0 0 330 512" aria-hidden="true" role="img" version="1.1"
    xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
    width="1em" height="1em">
    <path d="M305.913 197.085c0 2.266-1.133 4.815-2.833 6.514L171.087..."
    </svg>
    </span>
    </span>

    Can this be done automatically? Or would I have to put this into the filter? I guess, I would need a different code for the mobile menu.

    I understand, if this is beyond the scope of a support forum. I could live with two top level items, if the submenu would be too messy.

    #1874826
    David
    Staff
    Customer Support

    Hi there,

    just a suggestion but there is this filter:

    https://developer.wordpress.org/reference/hooks/wp_setup_nav_menu_item/

    Which allows you to change the Menu Title text ( amongst other things ) – and considering its only the text you want to change it may be better option to use this on a standard menu item, which should have no issues with the sub menu.

    Tried finding an easy example of it in use – and this is ‘simplest’ i could find:

    https://stackoverflow.com/a/62691239

    It should give you some idea of its usage.

    #1876648
    mkjj

    Hi,

    pretty good idea. I hadn’t thought of that. Your solution is better for this simple task.

    As always, magnificent support! Thank you!

    #1876968
    David
    Staff
    Customer Support

    Glad to be of help!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.