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

Securely log in a user without a password using a link?

Yes, you can. However, you’ll need to know either the userid or the username.

Create a link with any of that as a data attribute and make a ajax call with that. On success, you can just reload the page and your user will be logged in.

If you know the user id..

function wpse_304392_login_by_id() {

    $user_id = $_POST['user_id'];

    $user = get_user_by( 'id', $user_id ); 
    if( $user ) {
        wp_set_current_user( $user_id, $user->user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user->user_login );
    }

    wp_die(); //Essential for AJAX calls
}

If you know the username..

function wpse_304392_login_by_username() {

    $username = $_POST['username'];
    $user = get_user_by( 'login', username ); 
    $user_id = $user->ID;

    if( $user ) {
        wp_set_current_user( $user_id, $username );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $username );
    }

    wp_die(); //Essential for AJAX calls
}

Don’t forget do a nonce check to make it secure.

Related Posts:

  1. WordPress auto login after registration not working
  2. How can I secure a WordPress blog using OpenID from a single provider?
  3. My custom page template with is_user_logged_in() does not detect that I’m logged in
  4. Should I encrypt the response that triggers an Ajax action? Is nonce sufficient?
  5. Reset Password policy
  6. WordPress Login Customization for External Authentication
  7. How do you manage your pages or functions that require logged-in users?
  8. The same session information for peer users on two different WordPress servers
  9. Check for empty username or password on login
  10. WordPress – Security Question at Login from User’s Meta Data
  11. How to track all users logged into a site?
  12. How to authenticate/verify login credentials & check for user meta without logging in?
  13. Replacing the WordPress password validation
  14. Allow up to 5 Concurrent Login Sessions
  15. Log in from one wordpress website to another wordpress website
  16. Why does is_user_logged_in() return false after redirect from another site?
  17. Basic auth WordPress REST API dilemma
  18. security+best practices: root or www-data on a wordpress content folder?
  19. Any reason to be concerned by a wave of “zombie” blog signups?
  20. How to change WordPress user ID?
  21. Setting WP Admin passwords to expire
  22. Pre-populate Username Field
  23. How to keep track of user logins?
  24. Get user info outside WordPress
  25. Authenticate user using Hashed Password in MySQL Query and C#
  26. What do spammers gain by signing up as a user?
  27. Is it possible to get a user with just the password field?
  28. Redirect after login based on user role (custom login page)
  29. Check for user meta data at Login
  30. Redirect User to Homepage if no other redirect is specified
  31. Use phpbb user database for WordPress
  32. WordPress to use Drupal users’ credentials
  33. Authenticate with a Rails app?
  34. Share user table from WP with Drupal
  35. How can I allow password reset based on logins containing the @ character?
  36. A way to count logged in users and display count?
  37. stop login if user_status equal zero
  38. WordPress Authentication Middleware
  39. How do i make my wordpress website private?
  40. Redirect user to login before viewing custom post
  41. Change the user_login at registration
  42. Max no of simultaneous active sessions for a single user
  43. Rest API code to get ID of current user not working: get_current_user_id() gives 0
  44. Including users in insert link internal link search in Gutenberg
  45. When I try to login in wordpress it is showing “USER Doesn’t Exists”
  46. Force user to change their password on the frontend at the first login and password policy
  47. Redirect subscribers to last viewed page after log-in
  48. Are there mutiple ways to get usernames (as a hacker)
  49. Good way to block users within a multisite setup without deleting them?
  50. wordpress disable login for unverified user
  51. Rewrite Rules and Login Issue
  52. How can I authenticate user credentials against a WordPress instance?
  53. How do I protect user_activation_key?
  54. Is possible to allow user to login with different role?
  55. OAuth 2 and saving the authenticated user
  56. Failed login attempts
  57. Use WordPress Login for a non-wordpress site
  58. Allow users to create post without logging in?
  59. Separate Out Real Users
  60. Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
  61. New user form rejected because “passwords don’t match”—but there’s only one password field
  62. Is there any action /filter hook I can use to disable login for some user role?
  63. WordPress Password security related questions
  64. How to check User Role and redirect to specific page according to Role when login in WordPress?
  65. Preventing user enumeration: which logic is better?
  66. REST API: wp_get_current_user not working on second call
  67. Display video on homepage for users who have not logged in
  68. How to set all external links from a certain user to “nofollow”?
  69. How can i login with user’s password in WordPress being an admin?
  70. Delete a user from frontend
  71. Force users to use password with specifications
  72. How to make current user’s avatar + display name link to their profile?
  73. Where is the php file, that does the checks for login information?
  74. Check if user is logged in via JS? [duplicate]
  75. Use members from 1 site on another one
  76. Can I edit the database to change a login?
  77. how redirect users to custom login page when “login to reply” is clicked? [duplicate]
  78. User(s) already exists show error please provide a valid username
  79. wp_generate_password sets password but can’t login using created password
  80. add class to element if user is not logged in [closed]
  81. How to verify which WordPress user requested the API in ASP .NET Core?
  82. Need to use WordPress page as authentication for different service
  83. Is there a way to call via javascript if a user is logged-in on a static html file?
  84. Restricting wordpress login sessions for a web app
  85. How to allow access based on the user meta flag
  86. Authentication between two different sites using the WordPress login cookie
  87. I need a link that directs the user that is logged to his profile
  88. Use WP cookie to authentificate user on an external app
  89. How to connect wordpress user with my own APP user?
  90. How to change default username field after login
  91. Iterating users while user iteration is suppressed
  92. Disable emails for new user registration
  93. WP users cant reset password
  94. $user_login is not working if author has only 1 post
  95. Specific Content on pages based on user
  96. user and usermeta table not found
  97. throttle/limit a logged in user’s http requests to specific page on a per day basis
  98. Require confirmation of current user’s email before updating database and before send_email_change_email
  99. custom login form, guide me
  100. How to create a link to a user’s profile page
Categories users Tags authentication, links, login, security, users
Menu Underline shows without Hover after adding subitems
Increase product variation limit in woocommerce

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