Redirecting WordPress Users After Login

Redirecting WordPress Users After Login

7 Comments
Redirecting Users After Login
photo credit The Travelling Bum

There are a plenty of great reasons to support and encourage visitors to register and become members of your WordPress powered site, but let’s face it, you don’t always want them to land on the “Dashboard” page in WordPress’ backend once they login. This is particularly true if you are concerned about creating a seamless browsing experience for your guests.

Like many things in programming, there is more than one way to accomplish this depending on your specific needs, means and preferences.

On Page Login:

Let’s say you want users to be able to login from a link on a page and, once successfully logged in, be automatically returned to the page they started on. This is a scenario that could be useful if you have certain enhanced features and content for registered members or if you (or other editors) prefer to use WordPress’ on-page Edit link feature.

Adding this single line of code you can add to your theme’s appropriate template files (page.php, single.php, etc) will create a link that sends users to the standard login page but bounces them back to the referring page once they’ve logged in.

[code]Login[/code]

You can further enhance its appearance and placement with regular HTML and CSS.

Redirect to Home Page

Let’s say you want something a bit more flexible and don’t want to have to edit several of your theme’s template page files. One common post login action is to take visitors back to the site’s home page. To accomplish this, add the following snippet of code to your theme’s functions.php* file:

[code] add_action(‘login_form’, ‘redirect_after_login’);
function redirect_after_login() {
global $redirect_to;
if (!isset($_GET[‘redirect_to’])) {
$redirect_to = get_option(‘siteurl’);
}
}
[/code]

Redirect to Landing Page:

Let’s suppose that instead, you want members to be redirected to a specific page after they login. Perhaps a “Member News” page or a page with special offers just for them.

In your theme’s functions.php you can add the follow snippet of code:

[code] add_action(‘login_form’, ‘redirect_after_login’);
function redirect_after_login() {
global $redirect_to;
if (!isset($_GET[‘redirect_to’])) {
$redirect_to = get_option(‘sample-page’);
}
}
[/code]

You can replace sample-page with the name of your preferred landing page (essentially whatever comes after your root URL). In the example above you would end up at http://www.yoursite.com/sample-page/

* Standard disclaimer here. Before editing any critical files in WordPress you should make a backup and be prepared to roll back any changes in case you run into problems or conflicts.

Need help implementing this tip in your WordPress site? Contact us for our professional services.  We can also provide support & expertise in convenient “blocks” to suit your short and long term needs.

I'm the front-man of It's WordPress. I come from a diverse array of backgrounds, enjoying the opportunity to expand my knowledge base and skill set by re-inventing myself. I enjoy environments that focus on emerging information, technology and concepts. I put on the technical hat in my early 20s and never really looked back. I'm love technology and the internet, as well as the outdoors and avidly hike, kayak and camp every chance I get.

About Us

We can take you from concept, through design, development and deployment in one seamless process. Whether you choose a self-managed web site or need a continuing support relationship; we've got you covered.

CLICK TO EDIT

Request Consulation

Ready to transform your vision into a reality? Just looking to see what it takes to get the ball rolling. Tell us about your project and we can help. No spam. No obligation. Just answers.

WordPress Workflow With Rest API

Over the last decade we've seen the accelerating rise of dynamic JavaScript…
Continue reading

Three Must-Haves For Your WordPress Headlines

Simple headline best practices you can't afford to overlook. Don't bury the…
Continue reading

Hail to the King (of Content Management Systems)!

WordPress continues to grow as the CMS (Content Management System) of choice on…
Continue reading

Theme View – Cool Free Themes For WordPress Sites

In this post we look at three free themes that use the…
Continue reading

A Fist Full of Facebook… Plugins

Love or hate it, Facebook is still the 800 pound gorilla in…
Continue reading

Modifying the WordPress Admin Toolbar

The Admin Toolbar was introduced back in WordPress 3.1 (trivia points if…
Continue reading

Defer Ads by Date on WordPress Post and Pages

The topic of advertising on a website can be a sensitive one.…
Continue reading

Theme View – Cool Free Themes For WordPress Sites

In this Theme View update we feature themes that take advantage of…
Continue reading
7 Comments
    • Rodman Kettering
    • April 2, 2013
    Reply

    Fantastic items from you, man. I’ve bear in mind your stuff previous to and you’re simply too excellent. I really like what you have got here, certainly like what you are saying and the best way through which you say it. You’re making it entertaining and you continue to take care of to keep it smart. I can’t wait to read far more from you. This is actually a terrific web site.

    • Chris
    • February 1, 2013
    Reply

    Can you help me out here…

    How would I utilize this when coming from a different page? If coming from a different website, I would want to bring the user to wp-login and them redirect after they are logged in.

    What would I put on the end of the url to activate the “option”? My redirect for the login could be something like, WordPressSite.com/wp-login.php?option=SendMeBackAfterImIn

    That option would activate with your “if (!isset”

    I hope this makes sense. If not let me know what I missed.

    Thanks!

    • Shawn
    • May 23, 2012
    Reply

    Found this page with a Google search, and it’s nearly exactly what I was looking for, thanks! The only thing I’d like to know is if there’s an easy way to modify this so that admin users are excluded from the redirect…

    • Reply

      Hi Shawn, you could add some conditional comments that check the users’s account level, anything below Admin (or Editor, etc) gets sent to your home page (or any other location you want). If you are not comfortable with that route, I would recommend Peter’s (Excellent) Login Redirect Plugin. It has lots of options and can really fine tune the user login experience. The below is to the Official WordPress Plugin Repository (I don’t advise downloading plugins from other sources): http://wordpress.org/extend/plugins/peters-login-redirect/

      Best of luck!

    • affordable websites
    • May 9, 2012
    Reply

    Cool post thx..

  1. Reply

    That’s a great idea for an article. I’ll definitely put some thought into created a post about the advantages, drawbacks and tactics of registration.

  2. Reply

    Oh nice. I saw the title of the article and my first thought was “i wonder how you would redirect back to the article they came from”…and lo’ and behold, right there. 🙂

    Article suggestion: What’s the value in trying to drive registrations?

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

7 thoughts on “Redirecting WordPress Users After Login”

  1. Oh nice. I saw the title of the article and my first thought was “i wonder how you would redirect back to the article they came from”…and lo’ and behold, right there. 🙂

    Article suggestion: What’s the value in trying to drive registrations?

  2. Found this page with a Google search, and it’s nearly exactly what I was looking for, thanks! The only thing I’d like to know is if there’s an easy way to modify this so that admin users are excluded from the redirect…

    1. Hi Shawn, you could add some conditional comments that check the users’s account level, anything below Admin (or Editor, etc) gets sent to your home page (or any other location you want). If you are not comfortable with that route, I would recommend Peter’s (Excellent) Login Redirect Plugin. It has lots of options and can really fine tune the user login experience. The below is to the Official WordPress Plugin Repository (I don’t advise downloading plugins from other sources): http://wordpress.org/extend/plugins/peters-login-redirect/

      Best of luck!

  3. Can you help me out here…

    How would I utilize this when coming from a different page? If coming from a different website, I would want to bring the user to wp-login and them redirect after they are logged in.

    What would I put on the end of the url to activate the “option”? My redirect for the login could be something like, WordPressSite.com/wp-login.php?option=SendMeBackAfterImIn

    That option would activate with your “if (!isset”

    I hope this makes sense. If not let me know what I missed.

    Thanks!

  4. Rodman Kettering

    Fantastic items from you, man. I’ve bear in mind your stuff previous to and you’re simply too excellent. I really like what you have got here, certainly like what you are saying and the best way through which you say it. You’re making it entertaining and you continue to take care of to keep it smart. I can’t wait to read far more from you. This is actually a terrific web site.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top