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:857
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
- Scaling
- Constant
- Instructions
- 10–60
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_user_by().
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 61.
Third-party callbacks on 'auth_cookie' run inside this call, and their cost is not bounded by anything here.
1 place 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 - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | get_userdata() |
| always | 52 | get_userdata(), wp_hash(), hash_hmac(), apply_filters() |
| always | 60 | get_userdata(), ::WP_Session_Tokens(), ->create(), wp_hash(), hash_hmac(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 61 | 10–60 | 2 | |
| 8.5 | 61 | 10–60 | 2 | |
| 8.4 | 61 | 10–60 | 2 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 65 | 10–64 | 2 | |
| 8.2 | 65 | 10–64 | 2 | |
| 8.1 | 65 | 10–64 | 2 | |
| 7.4 | 65 | 10–64 | 2 |
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:
Uses · 5
- get_userdata()Retrieves user info by user ID.
- wp_hash()Gets hash of given string.
- hash_hmac()Compat function to mimic hash_hmac().
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Session_Tokens::get_instance()Retrieves a session manager instance for a user.
Used by · 1
- wp_set_auth_cookie()Sets the authentication cookies based on user ID.
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 ); } $pass_frag = substr( $user->user_pass, 8, 4 ); $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; $hash = hash_hmac( $algo, $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.
Signature, return type and hooks compared across 5 parsed releases.
$token parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.