wp_set_auth_cookie( int $user_id, bool $remember = false, bool|string $secure = '', string $token = '' )
- Since
- 2.5.0, 4.3.0
- Source
wp-includes/pluggable.php:1071
Description
The $remember parameter controls cookie persistence:
- If true, the cookie is persistent (default 14 days, filterable via 'auth_cookie_expiration').
- If false, the cookie is a browser session cookie (expires when the browser closes).
Internally, 'auth_cookie_expiration' is still applied, to expire the login after two days or when the browser is closed, whichever occurs first.
Compatibility
- WordPress
- since 4.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$user_idint- User ID.
$rememberbooloptional- Whether to remember the user.Default:
false $securebool|stringoptional- 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.Default:'' $tokenstringoptional- User's session token to use for this cookie.Default:
''
Performance profile
How much work a call to wp_set_auth_cookie() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 82–155
- Plugin surface
- 6 hooks
- Called by
- 3
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 170.
Third-party callbacks on 'auth_cookie_expiration', 'secure_auth_cookie', 'secure_logged_in_cookie' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below wp_set_auth_cookie() - serializeserialisation
maybe_unserialize()one call below wp_set_auth_cookie()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 24 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_set_auth_cookie() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$secure !== "" && $token !== "" && !apply_filters() | 82–86 | time(), apply_filters(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure === "" && $token !== "" && !apply_filters() | 85–89 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure !== "" && $token === "" && !apply_filters() | 90–94 | time(), apply_filters(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure !== "" && $token !== "" && !apply_filters() | 90–94 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure === "" && $token === "" && !apply_filters() | 93–97 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure === "" && $token !== "" && !apply_filters() | 93–97 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure !== "" && $token === "" && !apply_filters() | 98–102 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure === "" && $token === "" && !apply_filters() | 101–105 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters() |
$secure !== "" && $token !== "" && apply_filters() | 120–124 | time(), apply_filters(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token !== "" && apply_filters() | 123–127 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token === "" && apply_filters() | 128–132 | time(), apply_filters(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token !== "" && apply_filters() | 128–132 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
12 further outcomes, up to 155 instructions
$secure === "" && $token === "" && apply_filters() | 131–135 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token !== "" && apply_filters() | 131–135 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token !== "" && apply_filters() | 132–136 | time(), apply_filters(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token !== "" && apply_filters() | 135–139 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token === "" && apply_filters() | 136–140 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token === "" && apply_filters() | 139–143 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token === "" && apply_filters() | 140–144 | time(), apply_filters(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token !== "" && apply_filters() | 140–144 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token === "" && apply_filters() | 143–147 | time(), apply_filters(), is_ssl(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token !== "" && apply_filters() | 143–147 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure !== "" && $token === "" && apply_filters() | 148–152 | time(), apply_filters(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
$secure === "" && $token === "" && apply_filters() | 151–155 | time(), apply_filters(), is_ssl(), get_option(), parse_url(), apply_filters(), apply_filters(), ::WP_Session_Tokens(), ->create(), wp_generate_auth_cookie(), wp_generate_auth_cookie(), do_action(), do_action(), apply_filters(), setcookie(), setcookie(), setcookie(), setcookie() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 170 instructions, 82–155 executed per call, 7 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 7
7 hooks fire while wp_set_auth_cookie() runs, in this order:
- apply_filters( auth_cookie_expiration )filterline 1082 (+11 into the body)
Filters the duration of the authentication cookie expiration period.
- apply_filters( auth_cookie_expiration )filterline 1091 (+20 into the body)
Filters the duration of the authentication cookie expiration period.
- apply_filters( secure_auth_cookie )filterline 1110 (+39 into the body)
Filters whether the auth cookie should only be sent over HTTPS.
- apply_filters( secure_logged_in_cookie )filterline 1121 (+50 into the body)
Filters whether the logged in cookie should only be sent over HTTPS.
- do_action( set_auth_cookie )actionline 1154 (+83 into the body)
Fires immediately before the authentication cookie is set.
- do_action( set_logged_in_cookie )actionline 1171 (+100 into the body)
Fires immediately before the logged-in authentication cookie is set.
- apply_filters( send_auth_cookies )filterline 1189 (+118 into the body)
Allows preventing auth cookies from actually being sent to the client.
Uses · 6
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_ssl()Determines if SSL is used.
- get_option()Retrieves an option value based on an option name.
- wp_generate_auth_cookie()Generates authentication cookie contents.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Session_Tokens::get_instance()Retrieves a session manager instance for a user.
Used by · 3
- wp_setcookie()Sets a cookie for a user who just logged in. This function is deprecated.
- wp_signon()Authenticates and logs a user in with 'remember' capability.
- wp_update_user()Updates a user in the database.
Source code
function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) { 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.Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$token parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.