Languages: English • 日本語 (Add your language)
This function can be replaced via plugins. If plugins do not redefine these functions, then this will be used instead.
Changes the current user by ID or name.
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. wp_set_current_user() opens the ability to edit and perform actions on users who aren't signed in.
<?php wp_set_current_user( $id, $name ); ?>
Note that setting the current user does not log in that user. This example will set the current user and log them in.
$user_id = 12345;
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
Since: 2.0.4
wp_set_current_user() is located in wp-includes/pluggable.php
.
Current User Functions: get_current_user_id(), get_currentuserinfo(), wp_get_current_user(), wp_set_current_user(), current_user_can(), current_user_can_for_blog()