Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Auto Login After Registration

Here’s a basic approach.

First, you would need to pass something in the link to give some user info you can use to log the user in. To do this, you need to filter the email that goes to the user. (It would be possible to actually load a custom new user registration email, but this would have to be done as a plugin as that function is a pluggable function. Instead of doing that, this method just filters the email content based on the subject line for the registration email.)

// Add filter for registration email body

add_filter( 'wp_mail', 'set_up_auto_login_link' );

function set_up_auto_login_link( $atts ) {

    // if the email subject is "Your username and password"
    if ( isset ( $atts ['subject'] ) && $atts['subject'] = 'Your username and password' ) {
        if ( isset( $atts['message'] ) ) {

            $old = '/wp-login.php';
            $new = '/wp-login.php?user=" . $_POST["user_login'];

            $atts['message'] = str_replace( $old, $new, $atts['message'] );
        }
    }
    return $atts;
}

This will put the selected username from the registration form into the email in the form of a query string added to the login link. That username can be used to log the user in when they click the link.

To do this, I’ve hooked to the init action. This will check for the “user” parameter in the query string. If that exists, it uses get_user_by to get the user data by their username (login). If that returns a valid user, we can use the username and the retrieved user ID to log the user in:

add_action( 'init', 'log_user_in' );
function log_user_in() {
    if ( isset( $_GET['user'] ) ) {

        // get the username from the URL
        $user_login = $_GET['user'];

        // get the user data (need the ID for login)
        $user = get_user_by( 'login', $user_login );

        // if a user is returned, log them in
        if ( $user && ! user_can( $user->ID, 'manage_options' ) ) {
            wp_set_current_user( $user->ID, $user_login );
            wp_set_auth_cookie( $user->ID );
            do_action( 'wp_login', $user_login );
            wp_redirect( home_url() );
            exit();
        }
    }
}

Note, this process doesn’t give you any real security since anyone with a valid username could log in as that user. It does check to make sure the user being logged in is not an admin (user_can(‘manage_options’)) otherwise anyone with the admin user login could gain access. It would be wise to build in some additional checks – probably create a key or hash to add to the link as well, something done at registration that could be used to validate the user.

Related Posts:

  1. Separate registration and login for different roles
  2. SSO / authentication integration with external ‘directory service’
  3. How to prefill WordPress registration with social details
  4. Woocommerce registration page [closed]
  5. WordPress registration message
  6. How to remove the WordPress logo from login and register page?
  7. Login email after registration never sent or received
  8. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  9. How do I check if a post is private?
  10. Receiving “This content cannot be displayed in a frame” error on login page
  11. How to customise wp-login.php only for users who are setting a password for the first time?
  12. What hooks should I use for pre-login and pre-registration actions?
  13. Change register form action url
  14. Problem with logging in WP users automatically
  15. Is it possible a one click user registration with Facebook or Twitter (or other Social Networks)?
  16. Register/Login using only phone number?
  17. Force users to register in order to view website [duplicate]
  18. How do I force “users must be registered and logged in” on subsites?
  19. auto login after registeration for wp-members plugin
  20. How To Change Wp Register/Login URL Permanently To My Custom Page
  21. How to modify the action attribute of the wp-login.php?action=register form?
  22. Correct passwords keep appearing as incorrect
  23. Disabling standard registration login with username/email and password?
  24. Login form doesn’t log in
  25. Get the url of custom login page in the registration page
  26. By registering always make uppercase the first letter of the login
  27. Show reCaptcha on Custom Frontend Login & Register Form [closed]
  28. Best option to implement external register/login to WP from self-made API
  29. Disable all other page except index,register,login till user login
  30. What speaks against using a custom login.php / register.php to wordpress?
  31. How do I add Login fields and registration link to the header?
  32. How to make a user be able to register if such a login already exists?
  33. Sending new registration meta values to admin by email
  34. Are login functions considered part of the WP backend?
  35. WordPress registration page template
  36. Removing “public” user registration without completely turning it off?
  37. Disable registration on certain condition
  38. what is the best and safest way to allow users to register to site
  39. Updated : how to make email optional while user registration using default wordpress form
  40. Problem in auto login after registration
  41. How to invalidate `password reset key` after being used
  42. Updating usermeta from login redirect to billing address
  43. Chosen user password in registration is not being accepted on Login
  44. WordPress auto login user after registration only from a specific page
  45. User account activation links are lacking query strings
  46. Login user after registration programmatically
  47. How to get rid of the username of registration form in theme my login wp plugin?
  48. Where do I find “log in” and “register” link which are located on the top right corner?
  49. How to force login after user browses for a few minutes or browses a few pages?
  50. Registration and Login form
  51. WordPress and Magento: let WordPress manage user registration and logins?
  52. how to add custom word press regisration form in word press 3.5 with out module [closed]
  53. Click on banner to register to the blog
  54. How to put Login, Register and newsletter widget on the same page?
  55. make a login system for site visitors
  56. How to create a fully functional user registration in WordPress?
  57. In Django, how do I know the currently logged-in user?
  58. Check if wp-login is current page
  59. Can I programmatically login a user without a password?
  60. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  61. Is there any way to rename or hide wp-login.php?
  62. How to login with email only no username?
  63. How can I redirect user after entering wrong password?
  64. Increase of failed login attempts, brute force attacks? [closed]
  65. Login page ERROR: Cookies are blocked due to unexpected output
  66. Preventing session timeout
  67. How reduce wordpress login session timeout time?
  68. Check for correct username on custom login form
  69. Disallow user from editing their own profile information
  70. I can’t access my site via wp-admin
  71. ‘Password field is empty’ error when using autofill in Chrome
  72. Removing username from the ‘wordpress_logged_in’ cookie
  73. How to show ‘login error’ and ‘lost password’ on my template page?
  74. What is $interim_login?
  75. How to change “You must be logged in to post a comment.”
  76. Custom login form
  77. How to prefill the username/password fields on the login page
  78. wp_signon returns user, but the user is not logged in
  79. Adding extra authentication field in login page
  80. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  81. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  82. wp_signon() does not authenticate user guidance needed
  83. What exactly is ReAuth?
  84. What are the differences between wp_users and wp_usermeta tables?
  85. WordPress auto login after registration not working
  86. Login members using web services
  87. Make my wordpress blog remember my login “forever”
  88. How to check in timber if user is loggedin?
  89. How do I change the language of only the login page?
  90. Disable WordPress 3.6 idle logout / login modal window / session expiration
  91. Stop WordPress from logging me out (need to keep me logged in)
  92. How to disable autocomplete on the wp-login.php page
  93. Share login data/cookies between multiple installations
  94. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  95. How to pass users back and forth using session data?
  96. How do I change the logo on the login page?
  97. Why does WordPress hide the reset password key from the URL?
  98. Is it possible to sign in with user_email in WordPress?
  99. How to use current_user_can()?
  100. Avoid to load default WP styles in login screen
Categories login Tags login, user-registration
Connect to MySQL using Windows Authentication
Custom image size in the_content loop

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress