2

I am using Ultimate member plugin in WORDPRESS. And the plugin has functionality to switch user account on FRONT END, By clicking on the Login as this User but suddenly it stopped working, and Its not working.

I found the code in the files that it is ultimately using:

wp_set_current_user($user_id);
wp_set_auth_cookie($user_id, $rememberme );
// echo get_current user_id(); 
// here i am getting new user id;
$redirect_to = 'some url';
wp_safe_redirect( $redirect_to );
exit();

But after redirect i am not being switched to the new user but before redirection it returns new user id.

As i am not using any cache.

Thanks in Advance.

San Jay
  • 23
  • 1
  • 7

3 Answers3

0

The same trouble. Did not get any luck. I tried code like this:

global $current_user;
$ID = $current_user->ID;
unset($current_user);
nocache_headers();
wp_clear_auth_cookie();
wp_set_current_user($ID, $username);
do_action( 'wp_login', $username );
wp_set_auth_cookie($ID, true);

Maybe, it'll help someone to make something up))

I am not sure, but i guess we should clear old global value and old cookies.

PS I'm using BuddyPress plugin, but i don't think that it is important in this case.

Juljan
  • 2,391
  • 1
  • 17
  • 20
0

The answer is because you are redirecting and there is a bug in WebKit about setting cookies during a redirect response. You can work around this by allowing the page to fully respond and then triggering the redirect from javascript - or by having the page redirected to set the current user instead (perhaps from a session variable).

Safari Doesnt Set Cookie But IE-FF does

WebKit Bug Report

Community
  • 1
  • 1
bobbysmith007
  • 326
  • 2
  • 9
0

I faced a similar issue and worked on solving it for a couple of days. My solution was pretty interesting, I found out that the wp_set_current_user function was not working for me if it was called in a regular GET request. I was able to make it work only if I called it inside of a POST request.

I found this behavior weird and no other reference can be found to this on the web.

I hope it will help you guys.