Wordpress if_logged_in function on Non-wordpress p

2019-08-27 03:18发布

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!";
};
?>

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?

4条回答
beautiful°
2楼-- · 2019-08-27 03:37

You don't need to include the admin-header.php file. Just include this file:

wp-includes/pluggable.php

This file contains definition of is_current_logged_in() as well as many other user data helpers such as wp_set_current_user(), wp_get_current_user() etc.

查看更多
等我变得足够好
3楼-- · 2019-08-27 03:40

Including wp-load.php is the best way to go. There are loads of ways around this, but I use something similar to the following:

$wp_load_location_array = array();
$wp_load_location_array[] = "../../../../../../wp-load.php";
$wp_load_location_array[] = "../../../../../wp-load.php";
$wp_load_location_array[] = "../../../../wp-load.php";
$wp_load_location_array[] = "../../../wp-load.php";
$wp_load_location_array[] = "../../wp-load.php";

foreach($wp_load_location_array as $wp_load_location)
{
    if(file_exists($wp_load_location))
    {
        require_once($wp_load_location);
    }
}

It's a bit of a hack and if you actually know the location of the wp-load.php file, then you can just use that in the require_once() function.

Put that at the top of your file and you'll be able to use all of WordPress' functions in your non-wordpress page.

查看更多
对你真心纯属浪费
4楼-- · 2019-08-27 03:53

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!');
查看更多
男人必须洒脱
5楼-- · 2019-08-27 04:04

You have to use the Wordpress header because that function is in the core. The only other option is to write your own function with the same name, using the same database of users.

查看更多
登录 后发表回答