我已经运行了你的代码,除了包含在文件访问中之外,它对我来说运行得很好。php:
require_once($_SERVER[\'DOCUMENT_ROOT\'].\'/wp-load.php\');
启用调试以查看是否存在错误,这取决于其他问题,而不是这些特定代码行。
但是根据WP文件:actions reference 完成身份验证过程并设置cookie的早期操作挂钩是init
如中所述@Mohsin 所以我建议在文件访问中使用此代码。php
/* you probably don\'t need a theme */
define( \'WP_USE_THEMES\', false );
/* defining a constant here allows you to exit functions when not needed */
define(\'FROM_EXTERNAL_FILE\',true);
/* unless you\'ve renamed your wp-content folder this path will always point to wp-load.php even in subfolders installations */
require_once(explode("wp-content", __FILE__)[0] . "wp-load.php");
并移动签入文件函数。通过WP load加载整个WP环境的php。php
function is_logged_function(){
if ( !defined (\'FROM_EXTERNAL_FILE\') )
return;
if ( is_user_logged_in()){
echo "logged";
echo get_current_user_id();
}
else{
echo "not logged";
}
}
add_action(\'init\', \'is_logged_function\');