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:
- Refresh page after login with litespeed cache
- User registration followed by automatic login
- Adding “Remember Me” in custom login
- Nginx FastCGI_Cache Vs PHP Caching
- How to change the wp-login.php page title?
- How build a custom login/register form with error handling?
- Change the footer text on the login page
- Changing user_nicename
- WordPress 4 invalid username special charachters issue
- How to place login logout link on menu that redirects users back to current page?
- How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
- How to resolve error “Cookies are blocked due to unexpected output.”?
- Using a nonce in a Custom Login Form
- Constructing a custom login form using ajax
- automated tests as a user?
- How to clear WordPress Cache from Server/FTP/Remote location
- Remove username in emails or swap username for email
- woocommerce and is_user_logged_in() if not redirect to homepage
- PHP Code stuck in Cache [Memcached] [closed]
- Check if user is logged in when clicking certain links on certain pages
- PHP If user is logged in & on home page redirect
- WordPress shows registration link for non logged users
- Creating login session via CURL
- Admin username and password
- WordPress with php 5.5 and zend opcache
- Is it necessary to sanitize wp_set_password user input?
- WordPress custom login form using Ajax
- How to give new users two specific user role options upon WordPress user registration
- How to enable Zend Optimiser+ with Batcache
- Dynamic Stylesheet loads but doesn’t finish
- Wp-login appears White Screen, Error: Cannot modify header information
- How to: PHP Log Out Link?
- Login/logout in header
- Why is my cookie not unsetting upon logout? [closed]
- How to combine wordpress_logged_in cookies in one cookie?
- Programmatic Login from 3rd Party site
- Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
- How to debug my custom login form looping intermittently
- getting logged in user info (wp-load.php) from parent directory
- Passing UTM Parameters To Modify Page In WordPress
- WordPress & Fastcgi proper setup?
- Running code before object-cache.php runs
- Login to wordpress by clicking a link and specifying usernaname and password in url
- Log in / Log Out Custom Button
- Should `wp_login` be used since it’s deprecated?
- Change CSS based on is_user_logged_in
- How can I open up my administrative panel to everyone?
- customize wordpress database error page
- How can I add a new row in a separate database when someone registers via WordPress?
- WordPress transient not working with WP Engine
- Lost in trying to create user database system
- Shortcode to log user into current URL
- PHP getting error when trying to access WP-Admin Dashboard
- I installed WordPress locally now how do I login?
- If user is logged in not working
- Change homepage content if user is logged in – BuddyPress
- Make a page (url) not cacheable [closed]
- is_user_logged_in returning nothing on custom page
- Client cant see the Updated dynamic content
- Help with accessing wp-admin page and resolving error messages
- Admin Panel 404 Error after login
- PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
- How to block specific user id in custom login form?
- Log out without confirmation request (nonce)
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- How do I disable cache for a certain page?
- Language does not switch while cache is enabled
- Restrict wordpress access to logged users only
- Should I use wp_cache in my plugin to make it faster?
- Infinite loop when logging out using custom login form
- how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
- Issues adding Recaptcha v3 to WordPress Registration
- Display specific page if user signed in
- Redirect after login depending on the URL
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- Use WP Cron to Clear a Page Cache?
- is_user_logged_in() not working in homepage
- How save data on user’s client cache
- Check if a user is logged into my WordPress site which is on a different server
- Force ‘permanent’ post cache of shortcode results
- Newbie question. Login/Registration. New PHP page
- Adding a sidebar to wp-login.php
- Are WordPress widgets cached?
- List users in a dropdown for login
- Not logged in when using http
- add bootstrap modal after login in wordpress
- White screen after login attempt
- Splash pages on a high traffic site
- Redirect users not logged in to the standard login page (and back) from some posts and pages
- How to replace wp-admin login page to another location?
- Check user last login date
- How to change wp-admin and wp-login urls
- How do I do so that people can register on my wordpress site?
- wp_login_url always redirects me to wp-admin
- I can’t log into my website , it says “Error: Cookies are blocked due to unexpected output”
- How can I improve and optimise my wordpress web server for better performance in 2023
- change div text and link for logged in users
- wp-login – unable to redirect user to a custom login url
- Redirect to non-WordPress version of home page for search results via index.php customization overwritten
- change Dashboard default page on login