How to skip the activation page and send the user straight to the home page for WordPress Multisite

Recently I worked on a project for WordPress Multisite and I was asked if there is a way to skip the “wp-activate.php” page on user activation and instead, send the user to the homepage. On the home page, a “Welcome” message should be displayed, so the user knows that activation is ready and he/she can login right away. Well I though for a while and came up with a simple solution that worked. I think other people might need the same customization, so I decided to put together a simple tutorial to show you how to do it.

Prerequisites

Just to make sure we are on the same page before continuing with the tutorial, we are talking:

  1. WordPress Multisite
  2. After user registration the user gets activation email with activation link
  3. User click on the activation link
  4. User is redirected to the “wp-activate.php” page and presented with a message that the account is now active and User can login with username/pass.
  5. An email with the user credentials is send to User.

We will skip step 4 and instead of it we will add a step: User is redirected to “Fill in the page” and presented with a “Welcome” message.

Creating the Redirection Hook

For the purpose of catching the message before is handled by the “wp-activate.php”, I picked the ‘activate_header’ action to hook into and do the redirection. So I added this to the themes “functions.php” file:

What happens in the code block above is that we first hook to the ‘activate_header’ action with the function ‘check_activation_key_redirect_to_page’.

Inside the ‘check_activation_key_redirect_to_page’ function we first check if the activation “Key” is present and not empty. If it is not empty we check and activate the user with

$result = wpmu_activate_signup($key);

After the user is activated we get his information

$user = get_userdata( (int) $user_id);

and store it in a session

$_SESSION['rns_active_user'] = $user;

This is important because later we would want to retrieve this user information and may be personalize the “Welcome” message.

So now that we got the user activated and the info stored in the session, all we need is to redirect him to where we want to. In this case we sent him to the Network home page

wp_redirect( network_site_url() );
Add a hook to display the Welcome message

We are almost done, we have a new active user, he is on the Home page, now all we need is to show him the message. This is what I added in the same “functions.php” file:

What we do here is add a hook called

'my_do_active_user_message_hook'

In the hook we check and start the session again, and then check for the session variable we save the user info to

isset($_SESSION['my_active_user_variable']) && ! empty( $_SESSION['my_active_user_variable'] )

If we have the session variable, then the user must be coming from the activation page. This is our que to show him the “Welcome” message.

After the we show the user the message, we unset the session variable

unset($_SESSION['my_active_user_variable']);

since we are not going to need it again.

Final touch

We have all the parts, but one, set now. This part is actually very important. We need to add the display hook to our page. This can be done in different ways and you should do it and style the message accordingly to fit your theme. However, the easiest way to add it is to open the header.php file of your theme and add

<?php do_action('my_do_active_user_message_hook'); ?>

any where after the

<body>

tag.

That’s all it is to it. You can test around and fit the tutorial to your needs. I hope it is useful to somebody. As always if you have any questions, comments, feedback, let me know in the comments below.

Comments 11

  1. vishnu

    hello i need to skip step 3.User click on the activation link , after registration user need to automatically activate and login

  2. Maarten

    I was so happy to find this!
    But i can’t get it to work :-/

    My current theme gives an 500 error when redirected to wp-activate.php

    So I thought/hoped your code would help?
    But it still goes to wp-activate.php? (and displays the 500 error)
    Am I doing something wrong?

    1. Post
      Author
      vanbo

      Hi Maarten,

      Not sure what your setup looks like to tell if you are doing something wrong, but look at the debug.log file, you will most likely see why you get the error.

      If the problem is some code, then try to fix it. You can post the error here and I’ll try to help you with it.

  3. Tom

    Hi Vanbo,

    thanks for answering this fast!

    I’m desperate, because as soon as I add the code to header.php, I see php codes an the startpage of my main blog.

    On the mainblog I’m using a different standard theme as on the subblogs. I added all the codes to the theme of the main blog. Did I do that correctly?

    Thanks
    Tom

    1. Post
      Author
      vanbo

      Hi Tom,

      PHP code should be in placed between PHP tags ( open < ?php, close ?> ). Don’t duplicate the opening and closing tags, if you paste the “do_action” between php tags, you should not add additional ones.

      The do_action should be added to the theme, which will display the message.

  4. Tom

    Hi!

    I just tried what you explained. But when I add
    do_action(‘my_do_active_user_message_hook’);
    to the header.php, there is that exact part just written on the page.

    Haw can that happen?

    Thanks

    1. Post
      Author
      vanbo

      Hi Tom,

      you need to add the do_action surrounded by php tags ( < ?php do_action ?> ). Otherwise it is just text on the page.

    1. Post
      Author
      vanbo

      Hi Brad,
      it depends on where you want to redirect from, but if you are trying to redirect from the regular registration page, you can use the “registration_redirect” filter and add the link you want to redirect after registration.

  5. Denis

    Instead of redirecting to the Network home page, how would you redirect the user to their subdomain site home page? And would the Final hook added to the network sites’ theme header.php work if the network sites use a different theme than the network main page?

Leave a Reply

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