如何从外部 PHP 脚本获取 Wordpress 用户 ID?

How To Get Wordpress User ID From External PHP Script?

我正在尝试验证 WooCommerce 订阅,我需要用户 ID 来执行此操作,但它一直从 $current_user->ID.

返回 0
<?php
include('../wp-load.php');
$current_user = wp_get_current_user();
$SubCheck = WC_Subscriptions_Manager::user_has_subscription( $current_user->ID, 9, 'active' );
?>

你这个...

global $current_user;
$current_user = wp_get_current_user();
$current_user->ID 

试试这个方法,为什么 $current_user->ID

<?php $user_ID = get_current_user_id(); ?> 

Returns (int) 
The user's ID, if there is a current user; otherwise 0. 

查看:http://codex.wordpress.org/Function_Reference/get_current_user_id

编辑:

<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
}
?>

编辑2:

<?php global $current_user;
      get_currentuserinfo();
      echo 'User ID: ' . $current_user->ID;
?>

<?php 
global $current_user;
$current_user = wp_get_current_user();
$current_user->ID 
?>