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:
- 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
'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
hello i need to skip step 3.User click on the activation link , after registration user need to automatically activate and login
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?
Author
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.
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
Author
Hi Tom,
PHP code should be in placed between PHP tags ( open ). 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.
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
Author
Hi Tom,
you need to add the do_action surrounded by php tags ( ). Otherwise it is just text on the page.
I made a few changes to get it to work like I wanted it to. I’m working with the latest version of Multisite. Biggest change is to use cookies instead of session as sessions with WordPress can be hairy. Also, there’s a logic issue. Line 7 of check_activation_key_redirect_to_page should be an OR instead of AND.
https://gist.github.com/MikeNGarrett/9533303
Hey there, this is exactly what I need but not for multisite.
Any idea how to amend the code for normal WordPress sites?
Thanks
Brad
Author
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.
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?