# wp_generate_auth_cookie()

URL: https://chugunov.pro/api-wordpress/functions/wp_generate_auth_cookie/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.5.0.

## Сигнатура

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

## Описание

Формирует содержимое cookie аутентификации.

## Параметры

- `$user_id` `int` — обязательный. ID пользователя.
- `$expiration` `int` — обязательный. Время истечения срока действия cookie в виде временной метки UNIX.
- `$scheme` `string` — необязательный, по умолчанию `'auth'`. Схема cookie для использования: 'auth', 'secure_auth' или 'logged_in'.
  
  Значение по умолчанию 'auth'.
- `$token` `string` — необязательный, по умолчанию `''`. Токен сессии пользователя, используемый для этого cookie.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/pluggable.php:951`

```php
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 );
}
```

## История изменений

- 4.0.0 — The $token parameter was added.
- 2.5.0 — Introduced.

## Связанные

Использует: `WP_Session_Tokens::get_instance`, [`wp_hash`](https://chugunov.pro/api-wordpress/functions/wp_hash/), [`get_userdata`](https://chugunov.pro/api-wordpress/functions/get_userdata/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: [`wp_set_auth_cookie`](https://chugunov.pro/api-wordpress/functions/wp_set_auth_cookie/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_generate_auth_cookie/
