How to determine from an external app if user logged into WordPress?

Hi…

I have WordPress installed in one folder and I have a non-WP app installed in a sibling folder. I am trying to determine from the non-WP app if there is a user logged into WP.

In the WP folder I have created a WP load file wp-load-shortinit that loads a medium config of WP (more than just SHORTINIT but less than a full WP load). This file also echos to the screen status such as the current folder, number of users, and the user name if found to be logged in. This script does the following:

  • Loads WP
  • Echos number of users
  • Changes current working folder to the WP folder (if not there already)
  • Looks for a logged in user
  • Changes current working folder back to the original folder.

I can call wp-load-shortinit from the WP folder and everything works fine.

This is the wp-load-shortinit code

<?php

//echo "<p>Loading WP from {$wp_folder}</p>";

// Force a short-init since we just need core WP, not the entire framework stack
define( 'SHORTINIT', true );

// Build the wp-load.php path from a plugin/theme
//$wp_root_path = dirname( dirname( dirname( __FILE__ ) ) );
$wp_root_path = dirname( __FILE__ );
// Require the wp-load.php file (which loads wp-config.php and bootstraps WordPress)
require( $wp_root_path . '/wp-load.php' );

if ( SHORTINIT ) {
	// Because shortinit only loads very basic WP, we need to load additional functionality for things like user query.
	// This loads resources that are skipped in wp-settings when shortinit is set.
	require( $wp_root_path . '/wp-settings-shortinit.php' );
}


// Include the now instantiated global $wpdb Class for use
global $wpdb;


//WordPress is loaded
echo "<p>WordPress is loaded from " . __FILE__ . "</p>";


// Example: Retrieve and display the number of users and user name.
$user_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->users;" ) );
echo "<p>User count is {$user_count}</p>";

$user = wp_get_current_user();
if ( $user->exists()) {
	echo "<p>Welcome, {$user->display_name}! I see you are logged in.</p>";

} else {
	echo "Nobody is home.";
}

$user = ps_get_wp_user( $wp_root_path);

if ( $user->exists()) {
	echo "<p>The function ps_get_wp_user found {$user->display_name}!</p>";
} else {
	echo "The function ps_get_wp_user did not find a user.";
}


function ps_get_wp_user( $wp_root_path = NULL)
{
	if ( $wp_root_path == NULL) {
		$wp_root_path = getcwd();
	}

	echo "<p>Starting WP user lookup via function...</p>";

	$cwd = getcwd();
	echo "<p>>>>Starting in folder {$cwd}</p>";

	chdir( $wp_root_path );
	echo "<p>>>>Changing to WP folder {$wp_root_path}</p>";

	$wp_user = wp_get_current_user();

	chdir( $cwd);
	echo "<p>>>>Changing back to folder {$cwd}</p>";

	return $wp_user;
}

This is the good result from running the above script

WordPress is loaded from /home/wellnessstock/public_html/proddev/wp-load-shortinit.php

User count is 2285

Welcome, NormsIM! I see you are logged in.

Starting WP user lookup via function...

>>>Starting in folder /home/wellnessstock/public_html/proddev

>>>Changing to WP folder /home/wellnessstock/public_html/proddev

>>>Changing back to folder /home/wellnessstock/public_html/proddev

The function ps_get_wp_user found NormsIM!

So now I want to get that result when called from a script that starts in the non-WP folder. This non-WP script (wp-load-remote) does work in that it successfully calls wp-load-shortinit and the number of users is confirmed. But it fails to determine that a user is logged in.

**This is the wrapper code wp-load-remote in the non-WP folder that calls wp-load-shortinit **

<?php

# KEEPS THE PAGE FROM CACHING
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// Set folder that we want to load WP from.
$wp_folder = $_SERVER["DOCUMENT_ROOT"] . '/proddev/';

echo "<p>Loading WP from {$wp_folder}wp-load-shortinit.php</p>";
require_once $wp_folder . 'wp-load-shortinit.php';

This is the result from running the wp-load-remote script.

Loading WP from /home/wellnessstock/public_html/proddev/wp-load-shortinit.php

WordPress is loaded from /home/wellnessstock/public_html/proddev/wp-load-shortinit.php

User count is 2285

Nobody is home.
Starting WP user lookup via function...

>>>Starting in folder /home/wellnessstock/public_html/storedev/assets/includes

>>>Changing to WP folder /home/wellnessstock/public_html/proddev

>>>Changing back to folder /home/wellnessstock/public_html/storedev/assets/includes

The function ps_get_wp_user did not find a user.

Note that WP does get loaded and it properly returns the number of users in the db. But it fails to find the logged in user.

I’m looking for help with what I’m doing wrong and how to determine if the user is logged into WP.

I’m beginning to suspect that even though I changed the current working folder to the WP folder, because the originating URL address does not point to the WP folder, then access to the WP logged in cookie isn’t allowed.

If that’s the case, how would I go about doing this? Maybe a WP endpoint and do an GET call? (I’ve never done something like that so I’m clueless as to how to go about doing that.

Any help would certainly be appreciated!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.