This is a Q&A:
Recently I had a client come to me to build a new website. They had also forgotten all their login details, but they did have FTP access.
So, how do you create an admin user via code?
This will create an admin user if placed in a themes functions.php file. Please change the first three variables as needed.
/*
* Create an admin user silently
*/
add_action('init', 'add_user');
function add_user() {
$username = 'username123';
$password = 'pasword123';
$email = '[email protected]';
if (username_exists($username) == null && email_exists($email) == false) {
// Create the new user
$user_id = wp_create_user($username, $password, $email);
// Get current user object
$user = get_user_by('id', $user_id);
// Remove role
$user->remove_role('subscriber');
// Add role
$user->add_role('administrator');
}
}