Site logo

Archived topic

Login & Logout & My account

17 replies · Started by kvejonsio on October 26, 2016

Viewing posts 1–15 of 18

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;

}

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 :)

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

Thanks a lot Tom

Thanks for sharing it :)

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.

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;
}

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.

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

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

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 :)

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

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;
}

Glad to hear you found a solution

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?

This archived topic is closed to new replies.