跨WordPress站点会话同步问题:get_current_user_id()返回0
Hey there, let's work through this cross-site session sync issue you're having with your two WordPress sites. It’s frustrating when get_current_user_id() keeps returning 0 on one site, even though you’ve set up a page on the second to serve menu links based on the user’s session. Let’s break down the most likely fixes, step by step:
WordPress relies on cookies to track user sessions, so if your sites share a root domain (like site1.yourdomain.com and site2.yourdomain.com), misconfigured cookies are almost always the culprit. Add these lines to both sites' wp-config.php files:
define('COOKIE_DOMAIN', '.yourdomain.com'); // The leading dot lets subdomains access the cookie define('COOKIEPATH', '/'); define('SITECOOKIEPATH', '/');
Double-check that the domain matches your actual root domain—skip the leading dot only if your sites are on the exact same domain (unlikely for separate installs). Also, make sure both sites use identical authentication salt keys (the AUTH_KEY, SECURE_AUTH_KEY, etc., in wp-config.php). These keys encrypt the session cookie; if they don’t match, the second site can’t decrypt it to recognize the user.
If these are two sites in a single WordPress Multisite network, you can switch to the target site to fetch the user ID:
// Switch to the second site's ID (replace 2 with your actual site ID) switch_to_blog(2); $user_id = get_current_user_id(); // Switch back to the original site to avoid breaking functionality restore_current_blog();
Pro tip: This only works if the user is logged into the network (not just a single site). Enable shared user accounts in your Multisite settings if you haven’t already.
If your sites are completely separate (different databases), you’ll need a way to share session data between them. Here’s a practical approach:
- Create a shared database table that maps user session tokens to user IDs across both sites.
- On Site 1, when a user logs in, insert/update their session token and user ID into this shared table.
- On Site 2, hook into
initortemplate_redirect, check the session cookie’s token against the shared table, and manually set the current user ID if a match is found. - Don’t forget to add cleanup logic to remove expired sessions from the shared table (use WordPress’s native session expiration timestamps as a guide).
Security plugins like Wordfence, iThemes Security, or even your hosting provider’s firewall might block cross-site cookies. Disable these plugins temporarily to test if they’re causing the issue. If they are, look for settings related to "cross-site cookie sharing" or "SameSite cookies" and adjust them to allow your domain.
Modern browsers block cross-site cookies if the SameSite attribute is set to Strict. Update your wp-config.php to use a more permissive setting:
// For HTTP sites or mixed HTTPS/HTTP define('COOKIE_SAMESITE', 'Lax'); // For HTTPS-only sites (more secure for cross-domain) define('FORCE_SSL_ADMIN', true); define('COOKIE_SECURE', true); define('COOKIE_SAMESITE', 'None');
Note: None requires HTTPS on both sites—don’t use it if either site is on HTTP.
Use your browser’s dev tools (Application tab in Chrome/Firefox) to check if the WordPress logged-in cookie (usually named wordpress_logged_in_*) exists on both sites. If it only shows up on one site, your cookie domain/path settings are incorrect.
内容的提问来源于stack exchange,提问作者muhammad umar farooq frank

