WordPress - 检查用户是否从外部 PHP 文件登录 [英] WordPress - Check if user is logged in from external PHP file

查看:27
本文介绍了WordPress - 检查用户是否从外部 PHP 文件登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过外部 PHP 文件检查 WordPress 用户是否已登录,这是我的代码

I'm trying to check if WordPress user is logged in or not from an external PHP file and here is my code

<?php 
/*Load up wordpress externally*/
define( 'WP_USE_THEMES', false );
define( 'COOKIE_DOMAIN', false );
define( 'DISABLE_WP_CRON', true );
require('../../trading/wp-load.php');

if ( is_user_logged_in() ){
    $user =  wp_get_current_user();
    print_r($user);

}else{
    echo 'logged out';
}

?>

我总是收到已注销"消息,即使用户已登录.知道我该如何解决这个问题吗?

I always get 'logged out' message, even though the user is logged in. Any idea how can I fix this?

谢谢

推荐答案

我认为你的代码是正确的,所以我测试了它(只更改了require('../../trading/wp-load.php');" 以匹配我的安装)并且它按预期工作所以代码本身是正确的,而其他地方是错误的.

I thought your code was correct so I tested it (changing only the line "require('../../trading/wp-load.php');" to match my installation) and it worked as expected so code itself is correct and something else is wrong.

要让 WordPress(在标准配置中)知道用户已登录,它必须接收一个 cookie.你可以添加一个

For WordPress (in a standard configuration) to know a user is logged it must receive a cookie. Can you add a

var_dump( $_COOKIE );

到您的脚本并验证是否收到了像wordpress_logged_in_..."这样的 cookie.

to your script and verify a cookie like ''wordpress_logged_in_ ...' is being received.

WordPress 具有挂钩,因此可以自定义用户验证.当然,在定制安装中,上述情况可能并非如此.

WordPress has hooks so user verification can be customised. Of course in a customised installation the above may not be true.

这篇关于WordPress - 检查用户是否从外部 PHP 文件登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆