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.
Just to make sure we are on the same page before continuing with the tutorial, we are talking:
- WordPress Multisite
- After user registration the user gets activation email with activation link
- User click on the activation link
- 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.
- 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
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
since we are not going to need it again.
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
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.