Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roscabgdn/d6a709116ae90ad8beda81b60ed9fb27 to your computer and use it in GitHub Desktop.
Save roscabgdn/d6a709116ae90ad8beda81b60ed9fb27 to your computer and use it in GitHub Desktop.
Programmatically Create a User in WordPress
$user_name = 'username';
$user_email = 'email';
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
$user_id = wp_create_user( $user_name, $random_password, $user_email );
$user = get_user_by( 'id', $user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'administrator' );
} else {
$random_password = __('User already exists. Password inherited.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment