wppaste
WordPress

wp_generate_auth_cookie( int $user_id, int $expiration, string $scheme = 'auth', string $token = '' ): string

Since
2.5.0, 4.0.0
Source
wp-includes/pluggable.php:951
Generates authentication cookie contents.

Compatibility

WordPress
since 4.0.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.
$expirationint
The time the cookie expires as a UNIX timestamp.
$schemestringoptional
The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
Default 'auth'.Default: 'auth'
$tokenstringoptional
User's session token to use for this cookie.Default: ''

Return value

string
Authentication cookie contents. Empty string if user does not exist.

Performance profile

How much work a call to wp_generate_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
Heavy

Reaches the database via get_user_by().

Scaling
Constant

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

Instructions
10–67

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

Plugin surface
1 hook

Third-party callbacks on 'auth_cookie' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place 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
  • querycontent queryget_user_by()one call below wp_generate_auth_cookie()

Further down the call graph this can also reach option, cache, serialize 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 · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_generate_auth_cookie() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10get_userdata()
always56–59get_userdata(), wp_hash(), hash_hmac(), apply_filters()
always64–67get_userdata(), ::WP_Session_Tokens(), ->create(), wp_hash(), hash_hmac(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7010–674
8.57010–674
8.47010–67414 fewer instructions than PHP 8.3
8.38410–774
8.28410–774
8.18410–7742 fewer instructions than PHP 7.4
7.48610–794

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 · 1

One hook fires while wp_generate_auth_cookie() runs, in this order:

  1. apply_filters( auth_cookie )filterline 988 (+37 into the body)

    Filters the authentication cookie.

Uses · 5

Used by · 1

Source code

	function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {		$user = get_userdata( $user_id );		if ( ! $user ) {			return '';		} 		if ( ! $token ) {			$manager = WP_Session_Tokens::get_instance( $user_id );			$token   = $manager->create( $expiration );		} 		if ( str_starts_with( $user->user_pass, '$P$' ) || str_starts_with( $user->user_pass, '$2y$' ) ) {			// Retain previous behaviour of phpass or vanilla bcrypt hashed passwords.			$pass_frag = substr( $user->user_pass, 8, 4 );		} else {			// Otherwise, use a substring from the end of the hash to avoid dealing with potentially long hash prefixes.			$pass_frag = substr( $user->user_pass, -4 );		} 		$key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 		$hash = hash_hmac( 'sha256', $user->user_login . '|' . $expiration . '|' . $token, $key ); 		$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash; 		/**		 * Filters the authentication cookie.		 *		 * @since 2.5.0		 * @since 4.0.0 The `$token` parameter was added.		 *		 * @param string $cookie     Authentication cookie.		 * @param int    $user_id    User ID.		 * @param int    $expiration The time the cookie expires as a UNIX timestamp.		 * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.		 * @param string $token      User's session token used.		 */		return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );	}

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.0.0
The $token parameter was added.from the docblock
2.5.0
Introduced.from the docblock

About 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.