Unable to set $current_user in WordPress from external php page (not under WordPress CMS)

So I have a custom php page at http://vapeclubmy.com/store/survey.php that is independent from my WordPress/WooCommerce instance (installed in http://vapeclubmy.com/store).

I am trying to register a user and then authenticate and log that user into the vapeclubmy.com/store WordPress instance. Users are successfully registered with my current code but the session struggles to persist from survey.php to WordPress. When I add a product to cart using the ?add-to-cart=”ID” from this external page, the cart is empty, but it is actually added after i log into the account via the WordPress wp-admin.

Please have a look at my php code, not too sure what us wrong with it, I even had cookie set…

wp_create_user( $username, $password, $mail_id );
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
    echo $user->get_error_message();
    $user_id = 0;
    }else{
        wp_set_auth_cookie( $user->ID, true );
        wp_set_current_user($user->ID);
        $user_id = $user->ID;
        }

I’ve also done some checks via some function calls.

   <?php

 require('../wp-includes/pluggable.php');
require('../wp-blog-header.php');

global $current_user;

myFunction();

function myFunction(){

        wp_create_user( "example", "example", "example@gmail.com" );
        $creds = array();
        $creds['user_login'] = "example";
        $creds['user_password'] = "example";
        $creds['remember'] = true;
        $user = wp_signon( $creds, false );
        if ( is_wp_error($user) ){
    echo $user->get_error_message();
            $user_id = 0;
            }else{
            echo "SIGNED IN SUCCESS<br />";
        if(wp_set_auth_cookie( $user->ID, true )){
          echo 'cookie set';
        };
        if(wp_set_current_user($user->ID)){
          echo 'current user set!';
        }
        $user_id = $user->ID;
        }

 if(is_user_logged_in())
    {
    echo '<br />User Logged in ok<br />';
    echo 'User ID is: '.$user_id.'<br />';
    if($current_user){
    echo 'User login is: '.$current_user->user_login.'<br />';
    }else{
    echo 'no current user';
    }
}else{
   echo 'no user is logged in<br />';
}
 $user_ID= get_current_user_id();   

   echo"User number $user_ID is loggedin";
}

/*
global $current_user;
  get_currentuserinfo();

  echo 'Username: ' . $current_user->user_login . "n";
  echo 'User email: ' . $current_user->user_email . "n";
  echo 'User level: ' . $current_user->user_level . "n";
  echo 'User first name: ' . $current_user->user_firstname . "n";
  echo 'User last name: ' . $current_user->user_lastname . "n";
  echo 'User display name: ' . $current_user->display_name . "n";
  echo 'User ID: ' . $current_user->ID . "n";


$array = get_object_vars($array['data']);

echo json_encode($array['user_email']);
*/




?>

and these are the results returned

SIGNED IN SUCCESS

current user set!

User Logged in ok

User ID is: 6

no current user

User number 6 is loggedin

If someone can shed some light that would be amazing!

Read more here: Unable to set $current_user in WordPress from external php page (not under WordPress CMS)

1 thought on “Unable to set $current_user in WordPress from external php page (not under WordPress CMS)

  1. Hi, have question here, right now i use opencart as my website platform, i want to add a blog side in my website. is that any posibble for me to use wordpress as a platform adding to my website. thanks

Leave a Reply

Your email address will not be published. Required fields are marked *