I have a sub-domain which I'm trying to restrict to authenticated Wordpress users but the authentication isn't working. I've tried the following code:
global $current_user;
wp_get_current_user();
if ( is_user_logged_in() ) {
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
} else {
wp_loginout();
}
This works if I use the URL domain/intranet/page.php but not on intranet.domain/page.php How can I get it to work for subdomains? I've tried adding the following to wp-config.php but that hasn't done anything:
define('COOKIE_DOMAIN', '.domain'); // Used to authenticate subdomains
define('COOKIEPATH', '/');
Any ideas?
I have a sub-domain which I'm trying to restrict to authenticated Wordpress users but the authentication isn't working. I've tried the following code:
global $current_user;
wp_get_current_user();
if ( is_user_logged_in() ) {
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
} else {
wp_loginout();
}
This works if I use the URL domain/intranet/page.php but not on intranet.domain/page.php How can I get it to work for subdomains? I've tried adding the following to wp-config.php but that hasn't done anything:
define('COOKIE_DOMAIN', '.domain'); // Used to authenticate subdomains
define('COOKIEPATH', '/');
Any ideas?
Share Improve this question asked Feb 14, 2022 at 18:36 TomCTomC 1,3168 silver badges18 bronze badges 6- is this a multisite? Or two separate installs? Or one install that is not a multisite? If it does not work, how is it broken? Are you bootstrapping WordPress into standalone PHP files? It's highly unusual to directly visit a PHP file that didn't come from WordPress itself – Tom J Nowell ♦ Commented Feb 14, 2022 at 19:16
- It's standalone PHP files outside of wordpress. Trying to integrate existing web apps using the Wordpress authantication rather than 2 sets of user data. I know it's slightly unusual, but this is the requirement I'm faced with. – TomC Commented Feb 14, 2022 at 19:28
- you'll need to provide more information about where WordPress is located, if it's a multisite or a single site, etc, there's not enough information here to make a recommendation or write an answer, and you only answered the last question I asked and ignored the first 4 questions. If you do not know the answers please say so – Tom J Nowell ♦ Commented Feb 14, 2022 at 19:59
- 1 Sorry Tom - I thought it was clear from my response, but my mistake. The WP is a single site in the parent public_html dir (domain) and the web app is in a sub-domain (intranet.domain) which is just a pointer to a sub-directory (domain/intranet). So calling it in the sub-directory works, but when I call it using the subdomain URL it doesn't. When I say it doesn't work, I mean that the username is displayed as expected using the full URL but not when I use the subdomain URL. – TomC Commented Feb 14, 2022 at 22:12
- 1 And the expected cookie is there on the full URL but not on the subdomain URL. – TomC Commented Feb 14, 2022 at 22:12
1 Answer
Reset to default 0Ensure that COOKIEHASH
has the same value for both, e.g.:
define( 'COOKIEHASH', md5( 'https://yourdomain' ) );
define( 'COOKIE_DOMAIN', '.yourdomain' );
See https://wordpress.stackexchange/a/264490/736