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

Can’t programmatically log user in php

This modified version of your code works perfectly for me:

function test_login() {
    if ( empty( $_GET['uname'] ) || is_user_logged_in() ) {
        return;
    }

    $user = wp_signon( [
        'user_login'    => sanitize_text_field( wp_unslash( $_GET['uname'] ) ),
        'user_password' => 'password',
        'remember'      => true,
    ] );

    if ( is_wp_error( $user ) ) {
        wp_die( $user->get_error_message() );
    } else {
        wp_clear_auth_cookie(); // clear any user cookies set in the browser
        wp_set_current_user( $user->ID, $user->user_login ); // set current site user
        wp_set_auth_cookie( $user->ID ); // set user cookie
        do_action( 'wp_login', $user->ID, $user ); // trigger core action.
    }
}
add_action( 'after_setup_theme', 'test_login' );

Related Posts:

  1. Refresh page after login with litespeed cache
  2. User registration followed by automatic login
  3. Adding “Remember Me” in custom login
  4. Nginx FastCGI_Cache Vs PHP Caching
  5. How to change the wp-login.php page title?
  6. How build a custom login/register form with error handling?
  7. Change the footer text on the login page
  8. Changing user_nicename
  9. WordPress 4 invalid username special charachters issue
  10. How to place login logout link on menu that redirects users back to current page?
  11. How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
  12. How to resolve error “Cookies are blocked due to unexpected output.”?
  13. Using a nonce in a Custom Login Form
  14. Constructing a custom login form using ajax
  15. automated tests as a user?
  16. How to clear WordPress Cache from Server/FTP/Remote location
  17. Remove username in emails or swap username for email
  18. woocommerce and is_user_logged_in() if not redirect to homepage
  19. PHP Code stuck in Cache [Memcached] [closed]
  20. Check if user is logged in when clicking certain links on certain pages
  21. PHP If user is logged in & on home page redirect
  22. WordPress shows registration link for non logged users
  23. Creating login session via CURL
  24. Admin username and password
  25. WordPress with php 5.5 and zend opcache
  26. Recovering WP Login Credintials in Code?
  27. Is it necessary to sanitize wp_set_password user input?
  28. WordPress custom login form using Ajax
  29. How to give new users two specific user role options upon WordPress user registration
  30. How to enable Zend Optimiser+ with Batcache
  31. Dynamic Stylesheet loads but doesn’t finish
  32. Wp-login appears White Screen, Error: Cannot modify header information
  33. How to: PHP Log Out Link?
  34. Login/logout in header
  35. Why is my cookie not unsetting upon logout? [closed]
  36. User management system similar to wordpress one?
  37. How to combine wordpress_logged_in cookies in one cookie?
  38. Programmatic Login from 3rd Party site
  39. Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
  40. How to debug my custom login form looping intermittently
  41. getting logged in user info (wp-load.php) from parent directory
  42. Passing UTM Parameters To Modify Page In WordPress
  43. WordPress & Fastcgi proper setup?
  44. Dynamic content in template
  45. Running code before object-cache.php runs
  46. Problem with login form
  47. Login to wordpress by clicking a link and specifying usernaname and password in url
  48. Log in / Log Out Custom Button
  49. Should `wp_login` be used since it’s deprecated?
  50. How to redirect users based on role and content of redirect_to?
  51. Change CSS based on is_user_logged_in
  52. How to display login form anywhere, when user isn’t logged in, without redirecting?
  53. Is the wp_enqueue method efficient?
  54. How can I open up my administrative panel to everyone?
  55. customize wordpress database error page
  56. How can I add a new row in a separate database when someone registers via WordPress?
  57. WordPress transient not working with WP Engine
  58. One account with multiple logins
  59. Lost in trying to create user database system
  60. Shortcode to log user into current URL
  61. PHP getting error when trying to access WP-Admin Dashboard
  62. I installed WordPress locally now how do I login?
  63. If user is logged in not working
  64. Change homepage content if user is logged in – BuddyPress
  65. Make a page (url) not cacheable [closed]
  66. Show login greeting above sub-menu links?
  67. is_user_logged_in returning nothing on custom page
  68. Client cant see the Updated dynamic content
  69. Help with accessing wp-admin page and resolving error messages
  70. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  71. Can’t log in to WordPress wp-admin after adding code to functions.php
  72. Check if user had autologin & if so, logout
  73. Admin Panel 404 Error after login
  74. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  75. How to block specific user id in custom login form?
  76. Log out without confirmation request (nonce)
  77. Having trouble creating two shortcodes, one for logged in user and one for visitors
  78. Call WP Rest-Api to GET /users/me returned NOTHING in console
  79. How do I disable cache for a certain page?
  80. Language does not switch while cache is enabled
  81. Restrict wordpress access to logged users only
  82. Should I use wp_cache in my plugin to make it faster?
  83. Infinite loop when logging out using custom login form
  84. Menu not updating for logged in users after redirect
  85. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  86. Issues adding Recaptcha v3 to WordPress Registration
  87. Display specific page if user signed in
  88. Change Login or Logout text based on status
  89. Redirect after login depending on the URL
  90. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  91. Redirecting the lost password page request when using a custon login page
  92. Use WP Cron to Clear a Page Cache?
  93. How do I send a POST request with params with WordPress REST API
  94. wp-login.php?redirect_to=https problem
  95. is_user_logged_in() not working in homepage
  96. How save data on user’s client cache
  97. Check if a user is logged into my WordPress site which is on a different server
  98. Force ‘permanent’ post cache of shortcode results
  99. Newbie question. Login/Registration. New PHP page
  100. Fatal Error cannot log into wordpress and website disappeared
Categories PHP Tags cache, login, php
How to write ReWrite Rule to cut URL?
Sort posts based on an acf field called fecha value return longtext ‘20240517’

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