0

So I am trying to check if a user is logged in with the default WP function but it doesn't seem to work. Oddly, it works fine for people logged in (it echoes as it should) but it echoes nothing for people who aren't logged in. Anyone seen this behaviour?

if ( is_user_logged_in() ) {
    echo 'logged in';
} else {
    echo 'not logged in';   
}

This is put in the functions.php file

1

Depends on a few things... just using that code probably won't work if it doesn't know where to echo it. For example the below will output at the start of the loop on the frontend of the site, assuming your theme is using the loop...

add_action( 'loop_start', 'message_when_logged_in' );
function message_when_logged_in() {
    if ( is_user_logged_in() ) {
        echo 'logged in';
    } else {
        echo 'not logged in';   
    }
}

If that doesn't work try this since most all themes use wp_footer()

add_action( 'wp_footer', 'message_when_logged_in' );
function message_when_logged_in() {
    if ( is_user_logged_in() ) {
        echo 'logged in';
    } else {
        echo 'not logged in';   
    }
}
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.