'is_user_logged_in is not working outside of wordpress directory

I just can't seem to get is_user_logged_in working outside of my wordpress directory.

I have wordpress installed in mydomain/news/

If I execute for example mydomain/test.htm and have within the html an iframe where the source is within mydomain/news then the following code in that iframe works:

<?php
session_start();
define( 'WP_USE_THEMES', false ); 
Include_once($_SERVER['DOCUMENT_ROOT'] . '/news/wp-load.php');
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$username=$current_user->user_login;
}
?>

But I need to execute the test from within the mydomain/test.htm and not from within an iframe that is within mydomain/news

When I try the following code directly in mydomain/test.htm it does not return the $username - is_user_logged_in() returns false.

<?php
session_start();
define( 'WP_USE_THEMES', false ); 
Include_once($_SERVER['DOCUMENT_ROOT'] . '/news/wp-load.php');
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$username=$current_user->user_login;
}
?>

Has anyone any idea why I am not getting is_user_logged_in() to return true? - I am definitely logged in when executing the test code.



Solution 1:[1]

You mentioned: mydomain/test.htm, therefore unless your server is setup to execute .htm through PHP then it'll not work, you'll need to change the file format to .php.

Solution 2:[2]

The solution that I found was to add the following code to wp-config.php:

define('ADMIN_COOKIE_PATH', '/wp/wp-admin');
define('COOKIE_DOMAIN', '');
define('COOKIEPATH', '');
define('SITECOOKIEPATH', '');

Note that this code shall be added in the middle of wp-config.php, i.e. before the line: /* That's all, stop editing! Happy blogging. */. And you shall replace "/wp/wp-admin" depending on where your wordpress installation is located (in this example the main path is at /wp, and there is a subfolder /wp/wp-admin).

Solution 3:[3]

Maybe it just needs an IN

And in Sql Server it's best to avoid using Oracle date literals.
Use YYYY-MM-DD instead.

SELECT t.PDATE, t.PlantNumber
FROM [productline].[dbo].[Headertable] t
WHERE t.PDATE >= '2021-12-30 00:00:00' 
  AND t.PDATE <  '2022-01-06 00:00:00'
  AND t.PlantNumber IN ('test rig a', 'test rig b') 
ORDER BY t.PDATE DESC, t.PlantNumber ASC

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Script47
Solution 2 sissi_luaty
Solution 3