Created
June 7, 2016 08:20
-
-
Save roscabgdn/d6a709116ae90ad8beda81b60ed9fb27 to your computer and use it in GitHub Desktop.
Programmatically Create a User in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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