Skip to content

Instantly share code, notes, and snippets.

@wp-kitten
Created April 16, 2016 10:34
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 wp-kitten/978d730b27958b82fda62771d04153fd to your computer and use it in GitHub Desktop.
Save wp-kitten/978d730b27958b82fda62771d04153fd to your computer and use it in GitHub Desktop.
[WordPress] Create new admin account
function wpkAddAdminAccount()
{
$login = 'username';
$passw = 'password';
$email = 'email';
if ( !username_exists( $login ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $login, $passw, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}
add_action( 'init', 'wpkAddAdminAccount' );
@wp-kitten
Copy link
Author

Reset pass for existent users:

function wpkAddUpdateAdminAccount()
{
	$login = 'user';
	$passw = 'pass';
	$email = 'email';

	if ( !username_exists( $login )  && !email_exists( $email ) ) {
		$user_id = wp_create_user( $login, $passw, $email );
		$user = new WP_User( $user_id );
		$user->set_role( 'administrator' );
	}
	else {
		$wpkUser = get_user_by( 'email', $email );
		if( $wpkUser){
			wp_set_password( $passw, $wpkUser->ID );
		}
	}
}
//add_action( 'wp_footer', 'wpkAddUpdateAdminAccount' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment