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

WordPress login set cookie that survive browser exit (wp_signon function)

Without knowing what Google OAuth plugin you’re using (and how it internally signs a user in), the only way is to override the pluggable function wp_set_auth_cookie, setting $remember to always true:

if ( ! function_exists( 'wp_set_auth_cookie' ) ) :
    /**
     * Sets the authentication cookies based on user ID.
     *
     * The $remember parameter increases the time that the cookie will be kept. The
     * default the cookie is kept without remembering is two days. When $remember is
     * set, the cookies will be kept for 14 days or two weeks.
     *
     * @since 2.5.0
     * @since 4.3.0 Added the `$token` parameter.
     *
     * @param int         $user_id  User ID.
     * @param bool        $remember Whether to remember the user.
     * @param bool|string $secure   Whether the auth cookie should only be sent over HTTPS. Default is an empty
     *                              string which means the value of `is_ssl()` will be used.
     * @param string      $token    Optional. User's session token to use for this cookie.
     */
    function wp_set_auth_cookie( $user_id, $remember = false, $secure="", $token = '' ) {

        /**
         * Force $remember 
         */
        $remember = true;

        if ( $remember ) {
            /**
             * Filters the duration of the authentication cookie expiration period.
             *
             * @since 2.8.0
             *
             * @param int  $length   Duration of the expiration period in seconds.
             * @param int  $user_id  User ID.
             * @param bool $remember Whether to remember the user login. Default false.
             */
            $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );

            /*
             * Ensure the browser will continue to send the cookie after the expiration time is reached.
             * Needed for the login grace period in wp_validate_auth_cookie().
             */
            $expire = $expiration + ( 12 * HOUR_IN_SECONDS );
        } else {
            /** This filter is documented in wp-includes/pluggable.php */
            $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
            $expire     = 0;
        }

        if ( '' === $secure ) {
            $secure = is_ssl();
        }

        // Front-end cookie is secure when the auth cookie is secure and the site's home URL uses HTTPS.
        $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );

        /**
         * Filters whether the auth cookie should only be sent over HTTPS.
         *
         * @since 3.1.0
         *
         * @param bool $secure  Whether the cookie should only be sent over HTTPS.
         * @param int  $user_id User ID.
         */
        $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );

        /**
         * Filters whether the logged in cookie should only be sent over HTTPS.
         *
         * @since 3.1.0
         *
         * @param bool $secure_logged_in_cookie Whether the logged in cookie should only be sent over HTTPS.
         * @param int  $user_id                 User ID.
         * @param bool $secure                  Whether the auth cookie should only be sent over HTTPS.
         */
        $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );

        if ( $secure ) {
            $auth_cookie_name = SECURE_AUTH_COOKIE;
            $scheme="secure_auth";
        } else {
            $auth_cookie_name = AUTH_COOKIE;
            $scheme="auth";
        }

        if ( '' === $token ) {
            $manager = WP_Session_Tokens::get_instance( $user_id );
            $token   = $manager->create( $expiration );
        }

        $auth_cookie      = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
        $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );

        /**
         * Fires immediately before the authentication cookie is set.
         *
         * @since 2.5.0
         * @since 4.9.0 The `$token` parameter was added.
         *
         * @param string $auth_cookie Authentication cookie value.
         * @param int    $expire      The time the login grace period expires as a UNIX timestamp.
         *                            Default is 12 hours past the cookie's expiration time.
         * @param int    $expiration  The time when the authentication cookie expires as a UNIX timestamp.
         *                            Default is 14 days from now.
         * @param int    $user_id     User ID.
         * @param string $scheme      Authentication scheme. Values include 'auth' or 'secure_auth'.
         * @param string $token       User's session token to use for this cookie.
         */
        do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token );

        /**
         * Fires immediately before the logged-in authentication cookie is set.
         *
         * @since 2.6.0
         * @since 4.9.0 The `$token` parameter was added.
         *
         * @param string $logged_in_cookie The logged-in cookie value.
         * @param int    $expire           The time the login grace period expires as a UNIX timestamp.
         *                                 Default is 12 hours past the cookie's expiration time.
         * @param int    $expiration       The time when the logged-in authentication cookie expires as a UNIX timestamp.
         *                                 Default is 14 days from now.
         * @param int    $user_id          User ID.
         * @param string $scheme           Authentication scheme. Default 'logged_in'.
         * @param string $token            User's session token to use for this cookie.
         */
        do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );

        /**
         * Allows preventing auth cookies from actually being sent to the client.
         *
         * @since 4.7.4
         *
         * @param bool $send Whether to send auth cookies to the client.
         */
        if ( ! apply_filters( 'send_auth_cookies', true ) ) {
            return;
        }

        setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
        setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
        setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
        if ( COOKIEPATH != SITECOOKIEPATH ) {
            setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
        }
    }
endif;

I’d recommend putting this in a MU plugin, for example wp-content/mu-plugins/always-remember.php

Related Posts:

  1. How to override parent functions in child themes?
  2. wp_enqueue_script was called incorrectly
  3. Adding a second email address to a completed order in WooCommerce [closed]
  4. How to override a function when isn’t at functions.php
  5. Removing custom background and header feature in child theme
  6. Masking logout URL
  7. How can I tell if I’m on a login page? [duplicate]
  8. Is it possible to use a forgot password url filter?
  9. syntax for remove_filter in parent theme with class
  10. Is it possible to override this function/class in a child theme?
  11. How to prevent deleting of comments when deleting a post
  12. redirect wp-login.php to another page
  13. Convert hyphen to underscore in permalinks
  14. Login using the password from protected pages
  15. How to override filter in child theme?
  16. How do I redirect upon login a specific user based on role?
  17. Logging in redirects to correct page but shows logged out content until forced refresh
  18. Customized wp_new_user_notification
  19. change default option in wp_dropdown_categories
  20. What is the criteria for pluggable functions?
  21. Remove default user registration, login and subscriber profiles
  22. is_user_logged_in not working to redirect only logged out users
  23. Members only site – still need the lost password page accessible
  24. How to change login labels
  25. How can I get my Script to work on the Login page?
  26. Login Redirect if Logged in from Specific Page
  27. Unable to login using username
  28. When a user logs in, how can they view the website instead of the admin menu?
  29. WordPress PHP Conflicting User Sessions
  30. Get User Login Data (date, time… )
  31. Registration Hooks don’t appear to be working
  32. How to target post and pages and not all post types in admin?
  33. If user is logged-in display/hide something
  34. Secondary Menu and Logged In Users
  35. Prevent WordPress Automatic Logout
  36. error at login page in wordpress
  37. Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
  38. Logout Redirect and also WP-login.php Redirect
  39. overwrite code snippet from parent to child theme
  40. Solution dealing with Child Theme / Parent theme functions
  41. How to override this theme function in child theme
  42. loginout function customization
  43. Add Login and logout buttons to top menu bar
  44. Show errormessages on wrong username/password on custom loginform?
  45. This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
  46. Prevent wp_signon redirect on failed login for ajax login
  47. How to redirect Subscribers on login to specific page, when logging in from a Page
  48. How to replace ACTION url from original wordpress login form?
  49. Login cookies blocked after customizing hashing method
  50. Unable to login after registration
  51. I have an fatal error on my funcyions.php
  52. Allow logged in user to view a Page, else send to login screen and then redirect back to Page
  53. WordPress reading old version of functions.php, breaks site
  54. Redirect after login to current URL
  55. Providing fallback function and allow override by plugin
  56. Login functions
  57. WordPress permanently logging users out
  58. Integrate WP Tiles into existing loop (index.php) and theme
  59. save_post hook – headers already sent?
  60. How to request login for user but not for bots
  61. is_user_logged_in() isn’t working
  62. update_user_option not working as expected
  63. “Headers already sent” while trying to add a CSS file to my login page?
  64. Simple way to make most of my site private
  65. How can I edit the wp_parse_auth_cookie function from the pluggable file in my functions?
  66. wp_login_form display no styled form
  67. Redirecting after login except for a specific page
  68. Best collection of code for your 'functions.php' file [closed]
  69. Missing feature image link function
  70. What’s the difference between home_url() and site_url()
  71. Remove “Category:”, “Tag:”, “Author:” from the_archive_title
  72. get_template_directory_uri pointing to parent theme not child theme
  73. How to customize the_archive_title()?
  74. remove empty paragraphs from the_content?
  75. What is the “with_front” rewrite key?
  76. Why use if function_exists?
  77. Add multiple custom fields to the general settings page
  78. Ajax call always returns 0
  79. 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
  80. How long does a deprecated function live in core?
  81. Solution to render Shortcodes in Admin Editor
  82. How to add a data attribute to a WordPress menu item
  83. What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
  84. remove_action on after_setup_theme not working from child theme
  85. plugins_url vs plugin_dir_url
  86. Remove type attribute from script and style tags added by WordPress
  87. How to run a function every 5 minutes?
  88. Best way of passing PHP variable between partials?
  89. Upload Multiple Files With media_handle_upload
  90. How to display custom field in woocommerce orders in admin panel?
  91. Adding fields to the “Add New User” screen in the dashboard
  92. Issues with title-tag and document_title_parts
  93. How do I get the current edit page ID in the admin?
  94. How to check if a user exists by a given id
  95. Why isn’t is_page working when I put it in the functions.php file?
  96. Add tags to the section via functions.php
  97. Add image size if page template
  98. How to create a custom order status in woocommerce!
  99. Remove Actions/Filters added via Anonymous Functions
  100. How to load parent_theme functions.php before child_theme?
Categories functions Tags functions, login, oauth, pluggable
Remove the preview link box since 6.0 update
Random 404 errors using Buddypress

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