wppaste
WordPress

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:1067
Sets the authentication cookies for a given user ID.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
82–155

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 170.

Plugin surface
6 hooks

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.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get()one call below wp_set_auth_cookie()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
$secure !== "" && $token !== "" && !apply_filters()82–86time(), 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–89time(), 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–94time(), 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–94time(), 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–97time(), 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–97time(), 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–102time(), 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–105time(), 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–124time(), 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–127time(), 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–132time(), 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–132time(), 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–135time(), 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–135time(), 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–136time(), 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–139time(), 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–140time(), 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–143time(), 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–144time(), 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–144time(), 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–147time(), 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–147time(), 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–152time(), 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–155time(), 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:

  1. apply_filters( auth_cookie_expiration )filterline 1078 (+11 into the body)

    Filters the duration of the authentication cookie expiration period.

  2. apply_filters( auth_cookie_expiration )filterline 1087 (+20 into the body)

    Filters the duration of the authentication cookie expiration period.

  3. apply_filters( secure_auth_cookie )filterline 1106 (+39 into the body)

    Filters whether the auth cookie should only be sent over HTTPS.

  4. apply_filters( secure_logged_in_cookie )filterline 1117 (+50 into the body)

    Filters whether the logged in cookie should only be sent over HTTPS.

  5. do_action( set_auth_cookie )actionline 1150 (+83 into the body)

    Fires immediately before the authentication cookie is set.

  6. do_action( set_logged_in_cookie )actionline 1167 (+100 into the body)

    Fires immediately before the logged-in authentication cookie is set.

  7. apply_filters( send_auth_cookies )filterline 1185 (+118 into the body)

    Allows preventing auth cookies from actually being sent to the client.

Uses · 6

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.3.0
Added the $token parameter.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.