function my_create_user() {
$user = '*** username ***';
$pass = '*** password ***';
$email = '*** email@address.com ***';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' ); // Set user role ('editor', 'customer', 'subscriber', etc) More roles: https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles
}
}
add_action('init', 'my_create_user');
$creds = array(
'user_login' => 'example',
'user_password' => 'plaintextpw',
'remember' => true
);
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
$msg = $user->get_error_message();
}else{
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID ); // Set the current user detail
wp_set_auth_cookie ( $user->ID ); // Set auth details in cookie
$msg = "Logged in successfully";
}
<?php
function auto_login( $user ) {
$username = $user;
// log in automatically
if ( !is_user_logged_in() ) {
$user = get_userdatabylogin( $username );
$user_id = $user->ID;
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user_login );
}
}
?>
<?php
clean_user_cache($user_id);
wp_clear_auth_cookie();
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id, true, false);
$user = get_user_by('id', $user_id);
update_user_caches($user);
?>