X

How to Redirect After Login in WordPress

Snippets by IsItWP

Are you looking for a way to redirect users after login? While there’s probably a plugin for this, we have created some quick code snippets that you can use to redirect after login in WordPress.

Instructions:

All you have to do is add one of the following snippets to your theme’s functions.php file or in a site-specific plugin:

  • This snippet lets you redirect the user after they login. You need to change the ‘redirect’ parameter with the URL where you wish to redirect users after login.
    $args = array(
        'echo'           => true,
        'redirect'       => site_url( $_SERVER['REQUEST_URI'] ), // REDIRECT URL
        'form_id'        => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in'   => __( 'Log In' ),
        'id_username'    => 'user_login',
        'id_password'    => 'user_pass',
        'id_remember'    => 'rememberme',
        'id_submit'      => 'wp-submit',
        'remember'       => true,
        'value_username' => NULL,
        'value_remember' => false
    ); 
    wp_login_form( $args );
    
  • This snippet shows an alternate way of redirecting after the user logs in. This snippet redirects the user to the homepage after they login.

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress business themes for your website and how to easily create a multilingual WordPress website.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!