WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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_redirect( home_url() );
share|improve this question

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

 $html = "<script>
              location.href = " . $home_url() . "
 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 '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 '14 at 22:36
Yeah looks like I will need to use JS to redirect them to the log in page – user742736 Aug 14 '14 at 23:43

Your Answer


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.