You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中wp_get_current_user()函数失效问题求助

Troubleshooting wp_get_current_user() Failure in Custom PHP Pages (WordPress 5.2.3)

Hey there, since you confirmed other WordPress functions (like $product) work fine, that rules out issues with loading wp-load.php—so your hunch about cookies or sessions is spot on. Let's break down actionable fixes to get wp_get_current_user() working again:

WordPress relies on cookies to track logged-in users, and a mismatched cookie domain is the most common culprit here.

  • First, check your WordPress Address (URL) and Site Address (URL) in Settings > General—they need to match exactly (including HTTP/HTTPS and subdomains).
  • If you're using subdomains or HTTPS, manually set the cookie domain in wp-config.php:
    // For root + subdomains (add the leading dot)
    define('COOKIE_DOMAIN', '.yourdomain.com');
    // For HTTPS sites, enforce secure cookies
    define('COOKIE_SECURE', true);
    
    If your domain changes dynamically (e.g., staging vs production), use:
    define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']);
    

WordPress defaults to a root cookie path (/), but if your custom PHP page lives in a subdirectory, this can cause cookie mismatches. Force the root path in wp-config.php:

define('COOKIEPATH', '/');
define('SITECOOKIEPATH', '/');

3. Validate Session Initialization

Sometimes custom pages don't trigger WordPress's session setup properly. Try explicitly initializing the session before calling wp_get_current_user():

require( dirname( __FILE__ ) . '/../wp-load.php' );
require __DIR__ . '/../Connection.php';
include('config.php');

// Add this line before fetching the user
if (!session_id()) {
    session_start();
}

$current_user = wp_get_current_user();
if (! $current_user->exists() ) {
    header("location: " . LOGIN_URL);
}

Alternatively, you can directly access the global user object as a fallback:

global $current_user;
wp_get_current_user(); // Triggers user initialization
if (! $current_user->exists() ) {
    header("location: " . LOGIN_URL);
}

4. Check if WordPress Cookies Are Being Passed

Add a debug line to your custom page to see if logged-in user cookies are present:

var_dump($_COOKIE);

Look for cookies starting with wordpress_logged_in_—if they're missing, it confirms a cookie delivery issue. For HTTPS sites, you might need to adjust the SameSite attribute (WordPress 5.2.3 uses Lax by default):

// Add to wp-config.php for cross-site/HTTPS scenarios
define('COOKIE_SAMESITE', 'None');
// Ensure COOKIE_SECURE is true when using SameSite=None
define('COOKIE_SECURE', true);

5. Rule Out Caching Interference

Caching plugins (like W3 Total Cache, WP Rocket) can sometimes cache your custom page, stripping user-specific cookie data.

  • Add your custom page URL to the plugin's "exclude from cache" list.
  • Temporarily disable caching plugins to test if the issue resolves.

6. Debug the User Object Directly

Instead of just checking $current_user->exists(), dump the full user object to see what's going on:

$current_user = wp_get_current_user();
var_dump($current_user);
  • If you get an empty WP_User object: WordPress initialized the user but couldn't match it to a logged-in account (cookie issue).
  • If you get null: wp_get_current_user() isn't being initialized properly—double-check that wp-load.php is fully loaded before calling it.

内容的提问来源于stack exchange,提问作者Simone C.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:05:43