# wp_set_current_user()

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

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

## Сигнатура

```php
wp_set_current_user( int|null $id, string $name = '' ): WP_User
```

## Описание

Установите $id в null и укажите имя, если вы не знаете ID пользователя.
Часть функциональности WordPress основана на текущем пользователе, а не на вошедшем в систему. Поэтому появляется возможность редактировать пользователей, которые не вошли в систему, и выполнять с ними действия.

## Параметры

- `$id` `int|null` — обязательный. ID пользователя.
- `$name` `string` — необязательный, по умолчанию `''`. Имя пользователя.

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

`WP_User`

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

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

```php
function wp_set_current_user( $id, $name = '' ) {
	global $current_user;

	// If `$id` matches the current user, there is nothing to do.
	if ( isset( $current_user )
	&& ( $current_user instanceof WP_User )
	&& ( (int) $id === $current_user->ID )
	&& ( null !== $id )
	) {
		return $current_user;
	}

	$current_user = new WP_User( $id, $name );

	setup_userdata( $current_user->ID );

	/**
	 * Fires after the current user is set.
	 *
	 * @since 2.0.1
	 */
	do_action( 'set_current_user' );

	return $current_user;
}
```

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

- 2.0.3 — Introduced.

## Связанные

Использует: `WP_User::__construct`, [`setup_userdata`](https://chugunov.pro/api-wordpress/functions/setup_userdata/), [`do_action`](https://chugunov.pro/api-wordpress/functions/do_action/).
Используется в: `WP_Customize_Manager::_publish_changeset_values`, [`_wp_get_current_user`](https://chugunov.pro/api-wordpress/functions/_wp_get_current_user/), [`rest_cookie_check_errors`](https://chugunov.pro/api-wordpress/functions/rest_cookie_check_errors/), `WP_Importer::set_user`, [`wp_logout`](https://chugunov.pro/api-wordpress/functions/wp_logout/), [`set_current_user`](https://chugunov.pro/api-wordpress/functions/set_current_user/), `wp_xmlrpc_server::login`.

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