Ryan NZ
Ryan NZ

Reputation: 626

Check if wordpress user is logged in via external script

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?

Upvotes: 3

Views: 3594

Answers (2)

Ryan NZ
Ryan NZ

Reputation: 626

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

Upvotes: 1

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

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)

Upvotes: 3

Related Questions