Redirecting users on first login in WordPress
First published on December 24, 2011
On membership-based WordPress sites and other sites where you want to display a special welcome message or instructions to new users, you can implement some custom login redirect functionality. This functionality would kick in only once (or for the first few logins) per user.
The important elements on the code-side for such functionality is to use the built-in WordPress “login_redirect” filter, and to store information on whether or not the user has gotten the “first login” treatment. There are a couple of possible approaches to store the information, either in a cookie or in the user’s meta information (stored in the WordPress database in the “wp_usermeta” table).
Here is some sample code you can use in your theme’s functions.php file or in a plugin:
Cookie-based solution
// Send new users to a special page function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user ) { // URL to redirect to $redirect_url = 'http://yoursite.com/firstloginpage'; // How many times to redirect the user $num_redirects = 1; // Cookie-based solution: captures users who registered within the last n hours // The reason to set it as "last n hours" is so that if a user clears their cookies or logs in with a different browser, // they don't get this same redirect treatment long after they're already a registered user // 172800 seconds = 48 hours $message_period = 172800; // If they're on the login page, don't do anything if( !isset( $user->user_login ) ) { return $redirect_to; } $key_name = 'redirect_on_first_login_' . $user->ID; if( strtotime( $user->user_registered ) > ( time() - $message_period ) && ( !isset( $_COOKIE[$key_name] ) || intval( $_COOKIE[$key_name] ) < $num_redirects ) ) { if( isset( $_COOKIE[$key_name] ) ) { $num_redirects = intval( $_COOKIE[$key_name] ) + 1; } setcookie( $key_name, $num_redirects, time() + $message_period, COOKIEPATH, COOKIE_DOMAIN ); return $redirect_url; } else { return $redirect_to; } } add_filter( 'login_redirect', 'redirectOnFirstLogin', 10, 3 );
Download the cookie-based redirect on first login plugin
User meta table based solution
// Send new users to a special page function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user ) { // URL to redirect to $redirect_url = 'http://yoursite.com/firstloginpage'; // How many times to redirect the user $num_redirects = 1; // If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment // On a new site, you might remove this setting and the associated check // Alternative approach: run a script to assign the "already redirected" property to all existing users // Alternative approach: use a date-based check so that all registered users before a certain date are ignored // 172800 seconds = 48 hours $message_period = 172800; // If they're on the login page, don't do anything if( !isset( $user->user_login ) ) { return $redirect_to; } $key_name = 'redirect_on_first_login'; // Third parameter ensures that the result is a string $current_redirect_value = get_user_meta( $user->ID, $key_name, true ); if( strtotime( $user->user_registered ) > ( time() - $message_period ) && ( '' == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects ) ) { if( '' != $current_redirect_value ) { $num_redirects = intval( $current_redirect_value ) + 1; } update_user_meta( $user->ID, $key_name, $num_redirects ); return $redirect_url; } else { return $redirect_to; } } add_filter( 'login_redirect', 'redirectOnFirstLogin', 10, 3 );
Download the user-meta based redirect on first login plugin
Some extra notes:
- The code can be modified so that you don't actually redirect the user anywhere different than normal users, but set the user meta information or cookie and read that data to show a special pop-up or box to the user.
- WordPress has some default limitations disallowing redirects to URLs outside of your site's domain. If you need to redirect users elsewhere, you'll have to add the URLs to the "allowed redirect" list with code similar to this.
- If you have more sophisticated login redirect needs, you can adapt the code as an extension to this fully-featured redirect plugin.
January 4th, 2012 at 10:46 pm
Listof5 says:
Thanks for the tip. I am planning to open registration on my newly opened blog & was planning to use a welcome page for first time visitors. This code will help!
Thanks
January 5th, 2012 at 4:52 am
Anjali Jain says:
How do I get the username variable they used to login with? I’d like to make an IF statement so if the username equals something, it forwards them to the dashboard, anything else will redirect them to the homepage.
Reply from Peter: You can find the username in the $user object: $user->user_login. However, I’d also recommending checking out this more fully-featured plugin, which supports such a redirect out of the box.
January 5th, 2012 at 8:53 pm
James Ricardo says:
hi. Please help me about Peter’s Login Redirect plugin. How do I determine a fixed url in the plugin code? I just enter the id of the post. not want to put eg http:// ….. Excuse my English, I am Brazilian. thank you. I sent the comment here for the post of the comment plugin is closed
Reply from Peter: In version 2.4.0, you can use the syntax: “[variable]postid-23[/variable]” as the URL, replacing “23” with the ID number of the post you want to redirect to.
January 6th, 2012 at 7:18 am
James Ricardo says:
thanks for answering. interesting … but I wanted to make it easier for the User where he just put the id, and not have to enter [variable] postid-23 [/ variable] but 23. There’s no way in this part of the code and not in the wp panel?
Reply from Peter: You could certainly code that to support only numbers. You would have to replace the function rulRedirectFunctionCollection::rul_replace_variable in the plugin code, however.
January 6th, 2012 at 9:27 am
James Ricardo says:
almost got it. I replace rulRedirectFunctionCollection: rul_replace_variable by what code? I just want to insert the number in the field, I tried substituting for [variable] postid [/ variable] did not work. thank you
Reply from Peter: Sorry, you are going to have to write the code yourself. rul_replace_variable takes the entire string of the rewrite URL to match a particular pattern. In your case, it sounds like the rewrite URL is only the post ID, so you then fetch the permalink of the post with that ID within the rul_replace_variable function. Please use the forum for further questions and notes so that we can keep the thread on the current post on topic. Thanks!
February 8th, 2012 at 11:17 am
Philip says:
Hey Pete Luv the Plugin !
I am having a problem. Since I am entering my site from another web address http://freddy.com (using mask forwarding)- the standard WP login screen does not pop up when folks try to login. In IE it asks if I want to open another window ?
Also when I place the Logout redirect the plugin does not redirect on logout
Any thoughts ?
Thanks
February 8th, 2012 at 11:26 am
Philip says:
Apologize, I take that back on "logout" it works.
On the basic question of "login" tho Chrome kind stops, and IE says cannot open, unless in a new window.
It kinda makes sense to me since when you are LOGGED in your accessing a different domain name and content and therefore mask forwarding does not fully work.
I will check on the codex to see what I need to do to open a new tab on "Login"
Thanks great Plugin.
Reply from Peter: Yes, WordPress is very strict on forcing the domain that it is configured for. Also, in terms of logins in general, they are only ever valid for a domain and possibly its subdomains depending on the configuration, unless you set up a single sign-on mechanism.
June 15th, 2012 at 8:47 am
Jon says:
Hi. This plugin is exactly what I am looking for. I have a problem though, when I enter the login url for all subsciber categories, after login I get a 404 message. The url that the subscriber goes to is
http://finsecsuperhelp.com/www.finsecsuperhelp.com. The actual home page I want is www.finsecsuperhelp.com. Could you please help? Thanks.
Reply from Peter: Looks like you need to add “http://” to the start of your redirect URL
July 24th, 2012 at 5:38 pm
William says:
This plugin is great.
I would like to also be able to set my site so that if they went to www.mysite.com that they were redirected to www.mysite.com/login. This way the only people that can see the site is approved users.
Will this also stop search engines and robots from indexing my site?
Thanks
Reply from Peter: In other words, you want to force someone to log in to view your site? Yes, that means that search engines will not be able to index your site. There are some specific workarounds such as Google’s “First Click Free” that you can look up, but those are edge cases.
August 28th, 2013 at 7:58 pm
Geraldo Augusto says:
Great plugin.
But why not set the user meta information without checking passed time. If value is not set to 1, it is first login, we update it to 1, redirect to the page we want or execute some code.
I think it would be better and simple.
Reply from Peter: Good idea. There are some reasons for a time check, for example on a site with existing users. However, your solution is certainly more straightforward.
February 17th, 2014 at 2:40 pm
Johnny says:
Hi
I installed a plugin called "Wp-property" and when you’re logged in as admin, you can add property and browser path is: http://localhost/wordpress/wp-admin/post-new.php?post_type = property.
The idea is now a contributor log in, and should have access to this path.
how do I do it.
the contributor must be able to add property himself
Brg
Johnny
Reply from Peter: Sounds like a feature for another plugin that can manage roles and permissions. Good luck!
December 13th, 2014 at 10:39 pm
Graham says:
Hi Peter
We’re running an online dating site and would like to redirect members when they log in to a list of members on their preferred gender. So if a Woman is looking for a Man they would be redirected to the search results for all Men … these pages already exist but how do I set up such a redirection?
Thanks for you time.
Graham
Reply from Peter: You’ll have to write some custom redirect rules in PHP as described here
December 24th, 2014 at 3:17 am
Steve says:
Hi Peter,
I really like the plugin, thanks for the effort you must have put in to create it.
There’s something I trying to do which I hope you can help me with.
I want to redirect users to a different page in their profile.
When they get to the current page the url looks something like this:
http://example.com/members/the persons user login name here/whatever
I need some way of inserting, dynamically, the current user’s login name into the url. I’ve searched and tried to do this, but to no avail so far. I have a feeling the solution is in front of me, but I’m just not seeing it.
How would I achieve this? Any help would be appreciated.
Thanks.
Reply from Peter: You should be able to put [variable]username[/variable] in the redirect URL. My plugin converts that to the actual username.
January 13th, 2015 at 8:32 am
Gigi says:
If I wanted to have the redirect url be posted as a modal how would I go about doing this? Currently, I have lightboxes working with <a rel=lightbox> but I’m not sure how to add this to the code for the User meta table based solution.
Thanks!
January 13th, 2015 at 12:22 pm
Gigi says:
Also, I am using this feature along with your redirect plugin but when I add the code from the readme.txt (redirectOnFirstLogin) it’s not working. That is, I’m not getting the redirection on first login. I can send you over a temporary login for my staging site (http://koru2.staging.wpengine.com)
November 1st, 2015 at 7:40 pm
Stacv says:
Hello Peter, my friend and I are in the process of building a hosting web site. We will have members sign up, but we also will have host that will have their own page to login to. Our issue is how do we setup your plugin to work with wordpress in order to direct the host to their own page that we have designed for their use,
The host login and need to be directed to their own personal page, we can only allow one to do that right now, we need a lot more. What variable do we use? can you design one for us please.
Thank you
Stacy
Reply from Peter: What you are describing sounds like what this plugin does
October 17th, 2016 at 7:21 pm
Norman says:
Hello Peter, great plugin.
Please tell me, how can I adapt the code above to redirect a user when registered to a particular page. So immediately after registering it takes them to eg …edit-profile.
Thanks
Reply from Peter: You could hook into the registration_redirect filter.
November 3rd, 2016 at 11:01 am
Moke says:
Hello,
I used your plugin and it works fine. but I want to redirect users to temporary URL that expires after their first logins. I mean I don’t want "the first login page address" could be accessed in the future.
Thanks
Reply from Peter: Then you’ll need to code that into a template where you do something like check whether it is the user’s first login in that template and then don’t set the cookie or the meta table value until the user has loaded that page.
November 13th, 2016 at 10:35 am
moke says:
Hello again Peter,
I installed your plugin and pasted the above code in function.php. It works on wp login page. but I use "Login With Ajax" plugin and its widget for user’s login. It has its own login redirect option so the code is not working with it. As your plugin has the option to work with other plugin I used "http://www.mysite.com/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php" in "Login With Ajax" redirect path. Now After user login it tries to open "wplogin_redirect_control.php" but I get 404.
Can I Use external redirect file option for first login?
Thank you
Reply from Peter: The plugin won’t work for first login. You’d have to adapt the example code from this post and either hook into whatever the other plugin provides, or create a separate URL to handle that logic.
November 28th, 2016 at 7:20 pm
Sean says:
Hello Peter,
Sorry for the trouble, I tried both the meta plugin and the code directly in functions.php but nothing appears to be changing. The only change that was completed was the url to redirect to. Just want to make sure I am completing that correctly.
Thanks
Reply from Peter: Note that by default the example code provided also does an additional check that the user has registered in the past 48 hours.
November 13th, 2018 at 9:14 am
Tiam says:
Hello Peter,
Thanks for the plugin! It works great. I have a menu call "Dashboard", when user login it redirected to a page inside my website. However, while in redirected page if click on "Dashboard" again it logout immediately without warning. The only way to access the redirected page again is logout and login again. Can you suggest a way to fix that?
Thanks
Reply from Peter: I don’t think this would be caused by the redirect behavior specifically; perhaps there is some other dependency on logins or a subdomain that needs to be properly configured.
April 3rd, 2019 at 2:55 pm
Scott says:
Possible to add a check for user email? If email exists, do nothing. Otherwise, redirect to Woo My Account > Edit Account.
Reply from Peter: Sounds like you’re referring to some sort of custom workflow and/or account system. Probably doable, but you’d have to be familiar with the functions available for whatever system you’re using.
September 13th, 2020 at 9:18 am
Liz says:
Hi Peter,
I have activated the "Peter’s Login Redirect" plugin, and have also uploaded your Peter’s Redirect First Login Meta plugin. I have configured a role-based redirect for subscribers to go to their Profile page after login. However, I want to show a Welcome page to users that are logging in for the first time.
The settings on the main Peter’s Login Redirect page for the role-based redirect are working, but I’m not sure how to set up the first time login. Is it within the plugin editor itself? Or are these settings elsewhere?
Thanks,
Liz
Reply from Peter: Someone else now maintains the Peter’s Login Redirect plugin. For first-time login, you have to set the URL in the plugin file itself.
August 25th, 2021 at 4:01 pm
Tal Freibergs says:
I would like to use the redirect plugin. I would have to modify the code to redirect the user to the page that refers them to the registration page so that after they register (registration success logs them in automatically) they would be redirected back to the referring page. Do you have a snippet for that?
Reply from Peter: I no longer work on redirect plugins. You might try contacting the person who now maintains the redirect plugin https://profiles.wordpress.org/collizo4sky/