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

Check if user had autologin & if so, logout

Based on the suggestion of @JacobPeattie to use a cookie, I’ve constructed following working solution for this question:

/* LOGIN / LOGOUT */
/* --- */
/* AUTOLOGIN */
if( isset($_GET['username']) ) {
    $user = get_user_by('login', $_GET['username']);

    // Redirect URL //
    if ( !is_wp_error( $user ) ) {
        if ( in_array( 'customer', (array) $user->roles ) ) {
            wp_clear_auth_cookie();
            wp_set_current_user ( $user->ID );
            wp_set_auth_cookie  ( $user->ID );

            wp_redirect( '/lovelists/toon-lovelist/' );

            $autologin = 1;
            setcookie( 'autologin_status', $autologin, time()+31556926 , "/" );
            exit();
        }
    }
}

/* AUTOLOGOUT */
function log_out_user() {
global $wp;
$path = $_SERVER['REQUEST_URI'];
    if ( is_user_logged_in() && $_COOKIE['autologin_status'] == 1 && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
        wp_logout(); // this will logout user
        unset( $_COOKIE['autologin_status'] );
    }
}

add_action( 'init', 'log_out_user' );

Related Posts:

  1. How to: PHP Log Out Link?
  2. Login/logout in header
  3. Log out without confirmation request (nonce)
  4. Change Login or Logout text based on status
  5. User registration followed by automatic login
  6. Adding “Remember Me” in custom login
  7. How to change the wp-login.php page title?
  8. How build a custom login/register form with error handling?
  9. How to log out everywhere else, destroy all sessions “all other devices”?
  10. Change the footer text on the login page
  11. Changing user_nicename
  12. WordPress 4 invalid username special charachters issue
  13. How to place login logout link on menu that redirects users back to current page?
  14. How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
  15. How to resolve error “Cookies are blocked due to unexpected output.”?
  16. Using a nonce in a Custom Login Form
  17. Constructing a custom login form using ajax
  18. automated tests as a user?
  19. Remove username in emails or swap username for email
  20. woocommerce and is_user_logged_in() if not redirect to homepage
  21. Check if user is logged in when clicking certain links on certain pages
  22. PHP If user is logged in & on home page redirect
  23. WordPress shows registration link for non logged users
  24. Creating login session via CURL
  25. Admin username and password
  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. Wp-login appears White Screen, Error: Cannot modify header information
  31. Why is my cookie not unsetting upon logout? [closed]
  32. User management system similar to wordpress one?
  33. Programmatic Login from 3rd Party site
  34. Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
  35. How to debug my custom login form looping intermittently
  36. getting logged in user info (wp-load.php) from parent directory
  37. Problem with login form
  38. Login to wordpress by clicking a link and specifying usernaname and password in url
  39. Log in / Log Out Custom Button
  40. Should `wp_login` be used since it’s deprecated?
  41. How to redirect users based on role and content of redirect_to?
  42. Change CSS based on is_user_logged_in
  43. How to display login form anywhere, when user isn’t logged in, without redirecting?
  44. How can I open up my administrative panel to everyone?
  45. Force Users To Relogin
  46. How to hook a logout funtion for specific usr role in wordpress?
  47. How can I add a new row in a separate database when someone registers via WordPress?
  48. One account with multiple logins
  49. Lost in trying to create user database system
  50. Logout redirects to default page
  51. Shortcode to log user into current URL
  52. PHP getting error when trying to access WP-Admin Dashboard
  53. I installed WordPress locally now how do I login?
  54. If user is logged in not working
  55. Change homepage content if user is logged in – BuddyPress
  56. Show login greeting above sub-menu links?
  57. is_user_logged_in returning nothing on custom page
  58. Help with accessing wp-admin page and resolving error messages
  59. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  60. Can’t log in to WordPress wp-admin after adding code to functions.php
  61. Admin Panel 404 Error after login
  62. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  63. How to block specific user id in custom login form?
  64. Change button link to add nonce
  65. Having trouble creating two shortcodes, one for logged in user and one for visitors
  66. Call WP Rest-Api to GET /users/me returned NOTHING in console
  67. Restrict wordpress access to logged users only
  68. Infinite loop when logging out using custom login form
  69. Menu not updating for logged in users after redirect
  70. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  71. Issues adding Recaptcha v3 to WordPress Registration
  72. Display specific page if user signed in
  73. Refresh page after login with litespeed cache
  74. Redirect after login depending on the URL
  75. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  76. Redirecting the lost password page request when using a custon login page
  77. How do I send a POST request with params with WordPress REST API
  78. wp-login.php?redirect_to=https problem
  79. is_user_logged_in() not working in homepage
  80. Check if a user is logged into my WordPress site which is on a different server
  81. Newbie question. Login/Registration. New PHP page
  82. Adding a sidebar to wp-login.php
  83. List users in a dropdown for login
  84. Not logged in when using http
  85. add bootstrap modal after login in wordpress
  86. White screen after login attempt
  87. Redirect users not logged in to the standard login page (and back) from some posts and pages
  88. How to replace wp-admin login page to another location?
  89. Check user last login date
  90. Prevent users from display default wordpress login form
  91. login redirect based on user role not work as expected
  92. How to change wp-admin and wp-login urls
  93. How do I do so that people can register on my wordpress site?
  94. wp_login_url always redirects me to wp-admin
  95. I can’t log into my website , it says “Error: Cookies are blocked due to unexpected output”
  96. change div text and link for logged in users
  97. Unable to logout correctly after wp-login file was modified
  98. wp-login – unable to redirect user to a custom login url
  99. Redirect to current URL and append specified URL parameter on unsuccessful login through Elementor login form widget
  100. Can’t programmatically log user in php
Categories PHP Tags login, logout, php
Custom SQL query slows down when using multiple OR … LIKE … in posts_where filter
switch_to_blog() + do_action(‘generate_footer’) not working in multisite

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