Login & Logout links in the menu

Hello,

I want to add a login & Logout links to the menu. Also, redirect users after their login or logout to the Current Page.

I used this code, but it’s not stable. Some pages show login when it would be logout & vice versa.

add_filter( 'wp_nav_menu_items', 'wp_login_logout_link', 10, 2 );

function wp_login_logout_link( $items, $args  ) {
	if( $args->theme_location == 'primary' ) {
	        $loginoutlink = wp_loginout( 'index.php', false );
	        $items .= '<li class="menu-item">'. $loginoutlink .'</li>';
			return $items;
	    }
	    return $items;
}

Is there any stable code to achieve this and always redirect users to the Current page?

Thank You.

Hi @davidovic123, I don’t see a problem with your logic here… is there by chance some sort of caching involved? You might also check the response headers when the login / logout link is not correct, specifically Cache-Control and X-Cache-Hit (the latter specifically if you have a caching proxy such as squid).

You’re always redirecting to index.php, but you’ll want to redirect to the current page; this can be done using $wp-request (or even just $_SERVER['REQUEST_URI']):

function wp_login_logout_link( $items, $args  ) {
	global $wp;

	if( $args->theme_location === 'primary' ) {
		$loginoutlink = wp_loginout( home_url( $wp->request ), false );
		$items .= '<li class="menu-item">'. $loginoutlink .'</li>';
		return $items;
	}

	return $items;
}

Hello @m3g4p0p ,

When I Log In, the word & the link in the menu should say Log Out.

In fact, the Log Out shows correctly. When I browse the pages, the word displayed still the same in the same stand. But when I go to some pages the word in menu switch to Log In, when I’m already Logged In.

If I click on Log In, It’s bringing me to “my account” 's dashboard because I’m already Logged In. It does not bring me to Login Form.

I don’t understand why the word displayed in menu changes from “Log Out” to “Log In”.

Concerning caching I’m using the “W3Total Cache” plugin.

The “Log Out” link works well on pages, but for some of them it switches to “Log In”!!

How to get this stable and not changes over the pages?

Thanks.

This does indeed suggest that the page has simply been cached one way or the other. Try disabling your caching plugin and see if the problem persists; it might also be a possibility to force a hard reload and see if the menu updates then.

PS: Also, did you check the cache control headers?

Hello @m3g4p0p ,

Indeed, when I force a hard reload, the correct word appears in the menu.

Also, I checked my cache-control header, It reveals that there’s no cache-control:

cache-control : no-transform, no-cache, no-store, must-revalidate

Do you suggest to set up one?

How to avoid to always harden reload?

Thanks.

No that seems right… did you try disabling your caching plugin altogether though? I’m not familiar with W3TC, but from a quick look there appear to be options such as “Don’t cache pages for logged in users”… so if it works with the plugin disabled, you might then try fiddling with those settings.

Hello @m3g4p0p ,

Indeed when I disabled W3TC, Login pages are not cached and it works. No changes are mentioned in the Login/logout menu appearance.

Notice that In W3TC the Login pages are already not cached by default: “Don’t cache pages for logged in users” feature is enabled.

Beneath that feature, there is another feature for “users roles”. Admin, Author, Contributor are enabled to not caching by default.
I added all other users, such as subscribers, to no be cached.

I tested again and the previous behavior is come back despite the “not caching” is enabled.

Okay, well that was just a guess really from a first glance… you might have to look into the other settings as well. You probably don’t want to exclude the entire pages from caching anyway as this would render the use of that plugin moot I suppose; maybe you can just exclude that very fragment, or just the navigation menu.

For reference:

1 Like

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