简体   繁体   中英

Wordpress, get_current_user_id() returns 0 in a standalone php file

This is my standalone php file

<?php
include('wp-blog-header.php');
header("HTTP/1.1 200 OK"); 

global $wpdb; $user_id ;
$user_id = get_current_user_id();
echo $user_id;
?>

The php is giving the result as 0. Even if I am logged in with Id 1.

Include the wp-load.php file in place of wp-blog-header.php with correct path.

<?php
include('wp-load.php');

global $wpdb; $user_id ;
$user_id = get_current_user_id();
echo $user_id;
?>

I tried your exact code on my system and it works as expected so there is nothing wrong with the code itself. Can you try

 <?php
include('wp-blog-header.php');
header("HTTP/1.1 200 OK"); 
var_dump( $_COOKIE );
global $wpdb; $user_id ;
$user_id = get_current_user_id();
echo $user_id;
?>

The var_dump() should display a line like:

'wordpress_logged_in_ ....

If this cookie is not there WordPress will not see a logged in user.

How do you know you are logged in? Can you verify with the network tools of your browser that cookies are sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM