Jump to content

How to check if user is logged into wordpress on non-wordpress pages


Recommended Posts

I have used this tutorial here (https://codex.wordpress.org/Integrating_WordPress_with_Your_Website) as a guide to set up non-wordpress pages to access some wordpress resources on non-wordpress pages. Eg I can view wordpress posts on non-wordpress page. I have wordpress installed in a sub-folder(

./root - PHP pages

./root/wordpres - Wordpress installation}

 

In addition, I need to be able to detect if a user is logged into wordpress on the non-wordpress page. I am using this code below on non-wordpress pages for that:

if(is_user_logged_in()){
$current_user = wp_get_current_user();
/**
 * @example Safe usage: $current_user = wp_get_current_user();
 * if ( !($current_user instanceof WP_User) )
 *     return;
 */
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
echo get_avatar( $current_user->user_email, 32 ). '<br />';
echo '<hr />';  

}
else{
 echo 'Not logged-in';
}

Even though I am logged into wordpress, when I navigate to the non-wordpress page, I see wordpress posts listed but, the is_user_logged_in() function returns false and prints 'Not logged-in'!!!

 

Is there a way of achieving this besides the approach I have adopted?

I need guidance to resolve this.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.