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?