WordPress login redirect
Sometimes we require users to log in to a WordPress site in order to access front-end functionality hidden from guests. In such instances, we can simply provide a standard login link:
<a href="<?php bloginfo('url'); ?>/wp-login.php">log in</a>
While this gets the job done, it takes users to the dashboard after they have
logged in: they must then click on a link to return to the front-end, at which
point an additional click may be required to get them back to the page they
were viewing. Since WordPress 2.6.2 it has been possible to circumvent this
round trip from origin to wp-login.php to wp-admin/ to / and
finally back to origin by including a value for redirect_to in the href:
<a href="<?php bloginfo('url'); ?>/wp-login.php?redirect_to=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>">log in</a>
The above returns users to their starting point after they've logged in.
Possibly related posts
- Looping more than once with the WordPress loop
- Using HTML5 time element in WordPress themes
- Escape special characters for SQL REGEXP
- PHP print_filesize function
- Intelligent CSS caching
Comments
Hi. Where does one put this juicy bit of code?
@John If one visits /wp-login.php one is redirected to the dashboard after logging in. This may not be the way you wish a login link to operate. You may decide to hide the dashboard from your users entirely, in which case you will need to specify the page to which users are redirected by including a redirect_to value in the href of the login link.
awesome! I had been hoping to get this working - this made it possible. Thank you very much!
hi - great code, simple and works well.
one quick question - is it possible to do an exclude of the admin?
thanks!
Good question, Paul. In most cases it is possible to cater a site's behaviour to a user's role. This explanation of how to check if a WordPress user is an "administrator" is worth a look.
In this case, however, the user's role is unknown at the time the page (and thus the redirect_to argument) is rendered. Changing the behaviour for admin users would require performing a check during the login process, which is beyond the scope of this post.
Remember, though, that simply visiting http://yoursite.com/wp-login.php will ensure that you are taken to the dashboard once logged in.
I swear you just saved my life with this little piece of code. Hehehe. Thanks!
Ok, Here is a question. I built a login form on one of my pages, thus they dont use the standard login form. BUT is there a way I can redirect if they have a bad login?
Many thanks for this code which really saved me a lot of time and sweat. Taking users back to their original page is what most people expect (instead of the dashboard).
Hello and than you for this great guide! Added a nice touch to what i was trying to accomplish with the Simple Modal Login plugin (opens a login screen in lightbox fashion).
This trick worked great for my hardcoded links:
<a href="/wp-login.php?redirect_to=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>" class="simplemodal-login">
As you can see, my class 'simplemodal-login' is at the end. I would like to add the redirect and keep my simplemodal-login class, with this link structure:
<a href='".wp_login_url(get_bloginfo('wpurl'))."'>Login</a>."
Any advice would be greatly appreciated. Thanks and have a great one.
I haven't used WordPress for quite some time, so I can't offer any tips aside from those I've previously published.
By mistake i have installed a php script in my root directory, where wordpress is available. After that my all permalinks has been change. After modifying .htaccess file and modified file permission by ftp now My site site is almost ok. Now i can't log-in admin panel, when i am giving admin user name and password then url redirect to http://investorsbd.com/index.php/wp-login.php (extra index.php has shown) and nothing found for index php login. How can i remove that extra index.php for log-in my wordpress admin control panel?
Some how wp-log.php page has overtite, so how can a restore default wp-login.php page?
Thanks ng advance.