wp_set_current_user()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_set_current_user( int|null $id, string $name = '' ): WP_User
Описание
Установите $id в null и укажите имя, если вы не знаете ID пользователя.
Часть функциональности WordPress основана на текущем пользователе, а не на вошедшем в систему. Поэтому появляется возможность редактировать пользователей, которые не вошли в систему, и выполнять с ними действия.
Оригинал (английский)
Set $id to null and specify a name if you do not know a user’s ID.
Some WordPress functionality is based on the current user and not based on the signed in user. Therefore, it opens the ability to edit and perform actions on users who aren’t signed in.
Параметры
$id
int|null
обязательный
$name
string
необязательный
= ''
Возвращаемое значение
WP_User
Исходный код
wp-includes/pluggable.php:27
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. |

