Jump to content


Photo

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

wordpress

  • Please log in to reply
1 reply to this topic

#1 terungwa

terungwa
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts

Posted 25 January 2017 - 12:58 PM

I have used this tutorial here (https://codex.wordpr...th_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.



#2 Jacques1

Jacques1
  • Members
  • PipPipPip
  • Turtles all the way down
  • 4,224 posts

Posted 27 January 2017 - 10:57 AM

You have to make sure that the WordPress session cookie is also valid for your main application. Open the developer tools of your browser. What does the path attribute of the cookie say?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users