WordPress After Login Redirect Hook

Want to enhance your WordPress functionality using custom coding, then you are at the right place.  In this article, you are going to learn how to redirect on a custom page after a successful login in WordPress. When you are working on Woocommerce or any other type of application based on WordPress and you need to redirect your logged in user other than the default page or My Account page then you need to add few lines of codes in your functions.php.  Although there are many plugins available for this simple work you can create your own plugin using this successful login redirect code. WordPress is known for its simplicity and flexibility. We will use the feature of filters and hooks to extend the functionality.

Use hook wp_login()

There is wp_login() and we can use this function as a hook to create our own function and to redirect after login.

do_action( 'wp_login', string $user_login, WP_User $user )

 

Now we can write our own function to modify the default redirection. Suppose we have to redirect on the home page rather than the default page after successful login in WordPress. Then the code in the following example is sufficient :

function my_login_redirect() {
       wp_redirect(home_url());
        exit;
}
add_action( 'wp_login', 'my_login_redirect', 10, 3 );

 

Spread the love
  • 1
    Share

Leave a Reply

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.