It's WordPress!

Themes, Plugins, Tips, Tricks and More!

Redirecting WordPress Users After Login

Redirecting WordPress Users After Login

Posted by Geoffrey Fortier On November - 1 - 2011 Viewed:  40975
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.

<a title="Login" href="<?php echo wp_login_url(get_permalink()); ?>">Login</a>

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:

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

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:

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');
}
}

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.

Categories:

7 Responses to “Redirecting WordPress Users After Login”

  1. jascha says:

    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. 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.

  3. affordable websites says:

    Cool post thx..

  4. Shawn says:

    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…

    • 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!

  5. Chris says:

    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!

  6. Rodman Kettering says:

    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 Reply

Subscribe

All Topics
Featured
General News
Plugins
Themes
Tips & Tricks


 

Follow It's WordPress on Facebook Follow It's WordPress on Facebook Follow It's WordPress on Facebook Follow It's WordPress on Facebook