3

I am trying to check if a user has logged into WordPress via an external script.

Wordpress is located in the /blog/ directory. The external script is on the homepage (/index.php)

The code below returns an empty array when I am logged in.

define( 'WP_USE_THEMES', false );   
require_once $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-load.php'; 
if ( !function_exists( 'is_user_logged_in' ) ) { 
    require_once    $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-includes/pluggable.php'; 
} 

    $current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
    echo 'logged in';
}       

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 />';

However this returns the following empty values:

Username: 
User email: 
User first name: 
User last name: 
User display name: 
User ID: 0

After a bit of research, I have also tried the following in the config file which did not help.

define('COOKIE_DOMAIN', '.abc.com'); // your main domain
define('COOKIEPATH', '/');
define('COOKIEHASH', md5('abc.com')); // notice absence of a '.' in front

Any ideas why it is not returning anything?

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
Ryan NZ
  • 616
  • 1
  • 9
  • 25

2 Answers2

3

Following script works fine.

include('/var/www/html/wp2/wp-load.php');
//var_dump($_COOKIE);

global $current_user;
$current_user = wp_get_current_user();
var_dump($current_user->ID);
var_dump($current_user->display_name);

you should put the script on your WordPress root folder, then only WordPress can access the $_COOKIE and let you log in

More info for debugging: First, try to print $_COOKIE

var_dump($_COOKIE);

And confirm your script can access the WordPress $_COOKIE

it will look something like this

array (size=5)
    'wordpress_test_cookie' => string 'WP Cookie check' (length=15)
    'wordpress_logged_in_xxx' => string 'xx|xx|xxx|xxx' (length=125)
    'wp-settings-time-1' => string '1513059194' (length=10)
    'io' => string 'xxx-xx' (length=20)
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • Thanks for your message, just tested in in the /blog/ directory and can confirm it works fine. Any idea how to access it outside of the /blog/ directory? – Ryan NZ Dec 12 '17 at 06:43
  • 1
    It will not work outside of your domain path because `$_COOKIE`'s scope won't work outside of domain – Thamaraiselvam Dec 12 '17 at 06:46
  • 1
    After checking the Core of WordPress they set the user info from cookies, so when your script is outside of the scope of the cookie, WordPress cannot access it so it forces to WP_User with ID 0. – Thamaraiselvam Dec 12 '17 at 06:48
  • 1
    There is a way if you want to get them,put an script (should send you all users info ) inside /blog dir , and you can make the request to that script. – Thamaraiselvam Dec 12 '17 at 06:51
  • Thanks so much for your help. I didn't realise this would be an issue! Curious about your last point. I moved the script inside of the blog directory then made a request to it using require() (from /index.php), how it is still returning empty. Is there a better way to access this file to match the users cookie? – Ryan NZ Dec 12 '17 at 08:03
  • `require` won't help because the main script is you are running is outside of domain. I hope ajax request would help – Thamaraiselvam Dec 12 '17 at 08:08
  • Please accept the answer if this fixes your problem, i can still reply for your comments to fix other issues – Thamaraiselvam Dec 12 '17 at 08:09
  • this could help, https://stackoverflow.com/questions/3342140/cross-domain-cookies am not sure it works, but it would give you some idea – Thamaraiselvam Dec 12 '17 at 08:10
1

I managed to get this working. Make sure this is at the top of wp-config.php

define('COOKIE_DOMAIN', '.xxxfff.com'); 
define('COOKIEHASH', md5('xxxfff.com')); 

define('ADMIN_COOKIE_PATH', '/');
define('COOKIEPATH', '/');
define('SITECOOKIEPATH', '/');

Front end script

require_once $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-load.php'; 
global $current_user;
$current_user = wp_get_current_user();
//var_dump($current_user->ID);
//var_dump($current_user->display_name);
//var_dump($_COOKIE);


if ( !function_exists( 'is_user_logged_in' ) ) { 
    require_once    $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-includes/pluggable.php'; 
} 

    $current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
    echo 'not logged in';
} else {
    // Logged in.
    echo 'logged in';
}       
Ryan NZ
  • 616
  • 1
  • 9
  • 25