Skip to content

Instantly share code, notes, and snippets.

@hemraj7171
Created November 17, 2016 14:28
Show Gist options
  • Save hemraj7171/f2693919082ac78abae24dbfd97c3a62 to your computer and use it in GitHub Desktop.
Save hemraj7171/f2693919082ac78abae24dbfd97c3a62 to your computer and use it in GitHub Desktop.
Register new user on WordPress with role
/*
* Place this code on function.php or other included file on function.php
*/
//new user email
$email_address = 'hemraj7171@gmail.com';
//new manual password or generate password
//$password = wp_generate_password( 12, false );
$password = 'adminpassword';
//username
$username = 'hemraj7171';
//user role
$user_role = 'administrator';
//check username and email
if( null == username_exists( $username ) && !email_exists( $email_address ) ) {
//create new user
$user_id = wp_create_user( $username, $password, $email_address );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $username
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( $user_role );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment