WordPress.org

Ready to get started?Download WordPress

Forums

Function Reference/is user logged in not working (6 posts)

  1. hereintokyo
    Member
    Posted 3 years ago #

    I have never had the need to use this function, but am building a new site that will rely on it heavily. I have tried adding the following code to the header:

    <?php
    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, visitor!';
    }
    ?>

    This always returns "Welcome, visitor!" even if I am logged in in the same browser and same session. I have tried every browser I have, with no success.

    I tested this on some of my existing wordpress sites, and it works fine.

    The new site I am making is on a different server. The other things that are different are I have search engines blocked, and rather that have wordpress in the root folder, it is in a subfolder called "cms" but these do not seem to be causing any other problems.

    Any ideas or suggestions would be appreciated!

  2. anmari
    Member
    Posted 3 years ago #

    Hmm

    1) - it's a pluggable function - any other plugins altering the function?

    2) it just checks :

    $user = wp_get_current_user();
    	if ( $user->id == 0 )

    try doing $user = wp_get_current_user(); yourself and dumping $user to see what is going on?

    are you calling it in exactly the same place?

  3. hereintokyo
    Member
    Posted 3 years ago #

    Anmari, Thank you. I tried this

    $user = wp_get_current_user();
    $userid = $user->id;
    echo "User ID: " . $userid;

    But this always returns the "User ID: 0" even if I am logged in.

    My guess is this is an error caused by having my WordPress installation in a subfolder called "CMS" has the session cookies all have the path /CMS/ Next I will try moving my installation to the root.

    Thanks.

  4. anmari
    Member
    Posted 3 years ago #

    Hi, I don't think that's it.

    I don't think wordpress uses sessions? and Many of my sites are in a sub folder and I have had no problem with that. WordPress itself uses that function all over the place.

    Maybe somehow it thinks you aren't logged in yet? Page definitely refreshed after logon?

    what is the wp version? Do you have admin bar functionality? Switch on for front end and check whether it is displayed at the top ?

    The meta widget will also change if wp thinks you are logged in - drag it to the sidebar of the page where your code is and check that when you are logged in it says "logout"

  5. hereintokyo
    Member
    Posted 3 years ago #

    Thanks. I tried adding the meta widget as you suggested. It shows "login" even if I am logged in.

    In Firefox, if I delete the cookies for my site, and sign in, then there are new cookies. What makes me think having the installation in a subfolder is the problem is that the cookies show the path of the installation folder, not of the public side of the site.

    I will try moving my installation to see if that helps.

    Thanks.

  6. hereintokyo
    Member
    Posted 3 years ago #

    I moved my WP installation to the root rather than the subfolder CMS, and that worked. The cookies had the path /cms/ before the move, and now they have the path / which has fixed the problem.

    I consider this a bug in WordPress, since putting your installation in a subfolder causes core functions to stop working.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags