WJN
WJN

Reputation: 71

How to determine from outside of wordpress if a visitor is logged into Wordpress

Using php is it possible to determine if a visitor to my site is logged into the wordpress area of my site.

By this I mean the wordpress area of my site (which has registration) is in for example example.com/members/.

I would like to know from example.com/offers.php if they are logged into the wordpress area example.com/members.

Upvotes: 1

Views: 1630

Answers (2)

WJN
WJN

Reputation: 71

OK what seems to get overlooked is that when trying to access wordpress from a different folder to where wordpress is installed then using the following code does not work... is_user_logged_in() will always return false.

<?php
define( 'WP_USE_THEMES', false ); 
Include_once($_SERVER['DOCUMENT_ROOT'] . '/members/wp-load.php');
if ( is_user_logged_in() ) {echo ' You are currently logged in.'. '<br />';}
else
{    echo ' You are currently not logged in.'. '<br />';}
?>

I managed to find a workaround that for me at least works. I was building an html page that needed to test if the user was logged into the wordpress area on my site. So what I did was place the above test into a file that WAS in the wordpress folder. Then simply used an IFrame in my html that used as it's src= the file that was in the wordpress folder. That then echoes out what was need depending upon the user being logged in or logged out.

Probably a sloppy solution but at least it works.

Upvotes: 0

Lax Mariappan
Lax Mariappan

Reputation: 146

You can use WordPress functions outside too. Just include wp-load.php with your installation path.

use get_currentuserinfo() to get user data of logged in user.

// Include the wp-load'
include('YOUR_WP_PATH/wp-load.php');
if ( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo(); // to get currently logged in user data
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
}else{
   echo "User not logged in";
}

Upvotes: 4

Related Questions