xendi
xendi

Reputation: 2522

How To Get Wordpress User ID From External PHP Script?

I'm trying to validate WooCommerce subscriptions and I need the userid to do it but it keeps returning 0 from $current_user->ID.

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

Upvotes: 2

Views: 2050

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Try this way,why $current_user->ID

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

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

SEE:http://codex.wordpress.org/Function_Reference/get_current_user_id

EDIT:

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

OR

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

Upvotes: 3

ARWVISIONS
ARWVISIONS

Reputation: 106

thy this...

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

Upvotes: 2

Related Questions