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

Related posts

Leave a Reply

2 comments

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