0

I am using the following code to add Log In and Log Out links in the Primary Menu. Can't give a web link because the site is still under construction.

//* Add a Login/Logout toggle item in WordPress navigation menu
add_filter('wp_nav_menu_items', 'customprefix_add_loginout_navitem', 10, 2);
function customprefix_add_loginout_navitem($items, $args) {
    if ($args->theme_location == 'primary') {
        // Replace "primary" with your theme's menu name
        if (!(is_user_logged_in())) {
            $login_item = '<li class="menu-item login-menu-item"><a itemprop="url" href="/wp-login.php" rel="nofollow">Log in</a></li>';
        } else {
            $login_item = '<li class="menu-item logout-menu-item">' . wp_loginout($_SERVER['REQUEST_URI'], false) . '</li>';
        }
        $items.= $login_item;
    }
    return $items;
}

The problem is this: The logout link works properly in the membership area of the site, but if someone has logged in to the membership area and wants to browse to the normal public pages, the logout link randomly disappears on the public pages and is replaced with the login link. This means the user has to login again to get back to the membership pages. Not a good thing.

In other words, the logout link sometimes shows on the public pages and sometimes it doesn't. This happens most frequently on the home page, but in general it effects all public pages.

So, my question is:

After a user has logged in, how can I make the logout link show on all pages (both public & membership) until the user is ready to logout? I do not want to make them login again.

By the way, I tried at least 6 WordPress plugins to get the login/logout links to work properly before I went to the PHP option. All the plugins have had the same issue of not showing the logout link on all pages until the user is ready to log out.

Hope someone can help.

Thanks,

  • Are you enforcing HTTPS - as in, forcing everyone to use HTTPS all the time? It sounds like you may have a situation where HTTPS is enforced in wp-admin, but not being enforced in the front end, so if they happen to visit a HTTPS link on the front end they're still logged in and all is well, but if they happen to visit a HTTP link on the front end they're not logged in because HTTP is a completely separate site. In .htaccess you should be redirecting or rewriting all HTTP URLs to HTTPS if you are indeed using HTTPS. – WebElaine Oct 14 at 21:53
  • You bring up an interesting point and one that I had not considered. I am using the 'Really Simple SSL' plugin for the whole site. So all incoming HTTPS requests should be redirected from HTTP to HTTPS by that plugin. But this under construction site is currently living in a subfolder of the main site. Not sure if that would cause any issues - the main live site is SSL too. – Chris Oct 14 at 22:44
  • Subfolders likely won't make a difference, but I would do some testing and try to manually type in HTTP URLs to see whether it's possible to get there, or if it does redirect as intended. – WebElaine Oct 15 at 13:15
  • Thank you WebElaine for your suggestion. I did try typing HTTP URLs into all of the site pages, but each of them correctly redirected to HTTPS. I also have a couple of other ideas to try. If the problem still exists when the site is taken live, I will repost the problem with a link this time. – Chris Oct 17 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.