Written on 28. October 2021 by Lennart Christensen

Create a user programmatically in WordPress

add_action('init','create_user');

function create_user(){
$user = 'test';
$pass = 'password1234';
$email = '[email protected]';
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' ); // Could also be some other role
} }