[Resolved] Login & Logout & My account

Home Forums Support [Resolved] Login & Logout & My account

Home Forums Support Login & Logout & My account

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #239055
    kvejonsio

    Hi Tom

    I have added the code presented underneath in to generatepress-child -> functions.php, also have added the classes directly in the snippet, it works, but would you like to look on it and tell me if it’s a good solution, or maybe you know something 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 .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) .'">Log Out<span role="button" class="dropdown-menu-toggle" aria-expanded="false"></span></a>
    <ul class="sub-menu"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">My account</a></li></ul>
    </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;
    }
    
       return $items;
    
    }
    
    #239073
    Tom
    Lead Developer
    Lead Developer

    Looks good to me!

    For anyone else, the above function has WooCommerce specific functions (wc_get_page_id), so if you’re not using WooCommerce and use this code, you’ll get a fatal error.

    If you are using WooCommerce, it should work perfectly 🙂

    #239106
    kvejonsio

    yes, sorry I forgot to mention it, that code is for generatepress + woocommerce.

    Thanks a lot Tom

    #239107
    Tom
    Lead Developer
    Lead Developer

    Thanks for sharing it 🙂

    #240159
    Roman

    Hi there,
    just for your interest. If you add/modify the if-condition

    if ($args->theme_location == 'primary' || $args->theme_location == 'slideout')

    your login/logout should work also in slidout-menu.

    My question is: How can i make the Login/Logout appear on the right side of the primary-desktop-menu and make it appear on top of slideout-menu? As an perfect alternative, a behavior like the integrated search-icon would be nice.

    #240220
    Tom
    Lead Developer
    Lead Developer

    You can float it to the right like this: https://generatepress.com/knowledgebase/menu-social-icons/

    Not too sure what you mean by on top of the slideout menu? You can add it as the first menu item in the slideout menu?

    #1462811
    roadlink

    I added this snipped as below and it is working great.
    But this item can’t be shown in menu settings, so I can’t customize it or make it float right.
    I use Prime from site library which already has login | register link in the primary menu.
    but can’t change it like this

    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 class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Hesabım</a></li>
    </li>';
       }
       elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
       $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">GiriÅŸ Yap</a </li>';
       }
       return $items;
    }
    #1463429
    David
    Staff
    Customer Support

    Hi there,

    you can include your own custom CSS Class within the markup to target that for styling.
    For example – this line:

    <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">

    You can include your won class eg.

    <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page YOUR-CUSTOM-CSS-CLASS">

    If you want to try that – happy to help with any CSS if you can share a link to the site.

    #1915042
    Alec

    Hello,
    I have placed the code from above https://bit.ly/3Btd7zU in my child’s function.php and it works fine (except that the last two lines show an error, I had to remove them:
    return $items;

    }

    The question is: how can we enable this same dynamic Log-in log-out showing on mobile slide-out (off-canvas in my case) menu?
    Here is the URL: https://aleckatzmusic.com

    Thanks in advance

    #1915141
    David
    Staff
    Customer Support

    Hi there,

    this line in the code sets the condition to the primary menu:

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

    To include the Offcanvas (slideout) you would change it to:

    if (is_user_logged_in() && ( $args->theme_location == 'primary' || $args['theme_location'] == 'slideout' ) ) {

    #1915162
    Alec

    I get this error msg (line 212 is the line: if (is_user_logged_in() && $args->theme_location == ‘primary’) {
    that I change:

    Your PHP code changes were rolled back due to an error on line 212 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.

    syntax error, unexpected ”theme_location” (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{‘ or ‘$’

    forgive my lack of PHP knowledge 🙂

    #1915243
    David
    Staff
    Customer Support

    Oops – sorry about that, i updated the line above to include the missing )

    #1915927
    Alec

    Sorry, but there is a problem with this, I get an error, and also it stays “log-out” in the menu even if user is not logged-in.

    I tried and this one from here https://bit.ly/3Btd7zU

    combined with this line:

    if ($args->theme_location == 'primary' || $args->theme_location == 'slideout')

    works the best:

    /*Log-in Log-out in primary & slide-out menu*/
    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
       if ($args->theme_location == 'primary' || $args->theme_location == 'slideout') {
       $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">my account</a></li>
    </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;
    }
    #1916423
    David
    Staff
    Customer Support

    Glad to hear you found a solution

    #2147256
    Daniel

    Hi there!

    I was looking for a solution to rename the My Account link in GeneratePress if I’m logged out.

    I’ve used a custom top bar in Elements hooked to the before_header hook. Then I’ve used GenerateBlocks buttons to create a custom My account button with the user icon, so I’m not using a WP menu.

    Is it possible to change that GenerateBlocks button text in a similar way if user is logged out?

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