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

时间:2015-01-31 03:49:25

标签: php wordpress woocommerce

我正在尝试验证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' );
?>

2 个答案:

答案 0 :(得分:2)

你的......

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

答案 1 :(得分:2)

尝试这种方式,为什么$ current_user-&gt; 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.
}
?>

<强> EDIT2:

<?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 
?>