Darren Mathers Darren Mathers - 25 days ago 5
PHP Question

User is logged out when refreshing page

I am having issues with a Wordpress members site. The install is multisite with subdomains.

I require the user to be logged in to view all pages of the subdomain site. To do this I have opted against any functions and placed

<?php wp_login_form(); ?>
within the
page-template
as shown here...

<?php if ( is_user_logged_in() ) {
get_header(); ?>
<div class="page-content-wrapper ">
</div>
<?php get_footer();?>

<?php } else {?>
<?php get_header('login'); ?>
<?php wp_login_form(); ?>
<?php get_footer('login');?>
<?php }?>


This works a charm - when a
is_user_logged_in()
they get the page and when not they get
wp_login_form();
. My issue arises when leaving this page. The user is logged out and upon returning to the page has to login again as shown here.

Login error

This also happens when I add
if ( is_user_logged_in() )
to other pages.

I have also tried a redirect to
wp-login.php
when a user is required to login but this does the just loops back to
wp-login.php
which suggests to me that I am having a cookie issue as the login credentials logged.

I have noticed that before login the cookies are shown as shown

Cookies before login

and after login they show as shown

Cookies after login

Upon revisiting the page that requires login the cookies revert to the original. However, user credentials are never stored.

Any help would be appreciated.

UPDATE
It appears that if I first log into
wp-admin
I can visit the page and the user information is displayed. If I leave the page the user is logged out and a login is required.

Answer Source

There is a code issue in your footer.php line 38 :

<a role="button" href="<?php wp_logout(); ?> "

you are using wp_logout() that makes user logout when they visit the front-end of the site. you need to replace it by this :

<a role="button" href="<?php echo wp_logout_url(); ?> ">

this one is good as it will put the link in order to enable user to logout when clicking.