非 wordpress 页面上的 Wordpress if_logged_in 函数

问题描述:

我想以某种方式在非 wordpress 页面上使用此功能:

I want to somehow use this function on a non-wordpress page :

<?php
if (is_user_logged_in()){
    echo "Welcome, registered user!";
}
else {
    echo "Welcome, visitor!";
};
?>

有没有办法做到这一点?有人告诉我,如果我在非 wordpress 页面中包含我的 wp-header.php 是可能的,但我不想这样做.还有其他方法吗?

Is there any way to do this? I was told it is possible if I include my wp-header.php in the Non-wordpress page, but I do not want to do this. Any other methods?

您必须将 Wordpress Core 加载到当前脚本中.你可以这样做:

You have to load the Wordpress Core into your current script. You can do it like this:

    //wp-admin
    require $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';

    //Check if someone is logged in
    global $user_level;
    get_currentuserinfo() ;
    //echo "User Level = $user_level<br>";
    if ($user_level < 1)
            die('You aren\'t allowed to view this!');