Skip to content

Instantly share code, notes, and snippets.

@meetawahab
Last active September 28, 2019 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meetawahab/a3655632fdbbf814dbe8050b54f0096d to your computer and use it in GitHub Desktop.
Save meetawahab/a3655632fdbbf814dbe8050b54f0096d to your computer and use it in GitHub Desktop.
Create a new admin user in WordPress through code. You just need to change the variables and drop the file in the mu-plugins directory or add the following code in active theme's functions.php, then reload the homepage in WordPress. The new user will be created. Remove the file/code after that.
<?php
add_action( 'init', 'aw610_create_user' );
function aw610_create_user() {
$username = 'admin';
$password = 'password';
$email_address = 'email@domain.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
// To send an email to the new user.
wp_new_user_notification( $user_id, $password );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment