Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a custom shortcode that calls a function which will produce a registration page.

Registration works. No issue here.

The issue I am having is to log the user in after a successful registration.

I understand the reason I am getting header already sent error is because the shortcode is loaded after the header information.

What is the best way to redirect or log the user in within a shortcode

add_action( 'user_register', array($this,'auto_login_new_user'), 10);

function auto_login_new_user( $user_id ) {
    // exit($user_id);
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);

    wp_redirect( home_url() );
    exit;
}
share|improve this question

1 Answer 1

You have to rely on javascript for the redirect. You can set it like this in your shortcode.

 $html = "<script>
              location.href = " . $home_url() . "
          </script>";
 return $html;

It will load the page and then redirect immediately.

share|improve this answer
    
Thanks, so I guess there is no way to log them –  user742736 Aug 14 at 22:22
    
@user742736 You can log in a user with wp_set_auth_cookie(), but it has to be before headers are sent. So, in this case you can't. Why don't you redirect them to the Login page instead? –  sakibmoon Aug 14 at 22:36
    
Yeah looks like I will need to use JS to redirect them to the log in page –  user742736 Aug 14 at 23:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.