Home / functions.php / Redirect user after login
  • How to Redirect user after login

    redirect-user-after-login

    Adding this snippet to the functions.php of your wordpress theme will redirect the user to your homepage after they login.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    siteurl, add_action, get_option,
  • Marlon Amancio

    Tanks!!! I was looking for this.

    • Cool glad I could help, you should subscribe to our rss feed or join our Facebook group and check out the snippets we add everyday.

  • That’s a cool bit of code, how about if the user is Subscriber or if is a custom Role that would be useful for a project I’ve been working on recently. Basically if a user is a certain role they are redirected to a specific page?

    • Sure you could do that easy enough give me a few ill email it to you.

      • marcuswickes

        Can you send me this snipp that you said you would email to Elliot?

      • sam

        I could also use this code for role specific redirection. How can I get it? Thanks.

  • Ally

    This is brilliant, thanks so much for sharing! I was wondering if it’s possible to take it a step further and have the user redirected to a page specifically for them? for example: each user has a page created in their name and after login they are redirected to that specific page? Maybe a custom field could be used to differentiate the pages?

    My knowledge of php and wordpress doesn’t quite stretch this far, so pointers would be really helpful.

    Thanks

    • Hi Ally, sure you could do this, if you auto create a page for each user you could just append to the url eg:

      get_option(‘siteurl’).’/username/’;

      this type of thing, cheers.