- Add the following code to your themes functions.php file.
// Add a new admin user to WordPress on initialization
add_action('init', 'ec_add_new_wp_user');
function ec_add_new_wp_user() {
// For enhanced security, use a difficult-to-predict username
$username = 'blaEuzvTvW';
// Generate a random password
$password = wp_generate_password();
// Set other user credentials
$email_address = 'your@email.com';
// Check if the username already exists
if (!username_exists($username)) {
// Create a new user
$user_id = wp_create_user($username, $password, $email_address);
// Get the user object
$user = get_user_by('ID', $user_id);
// Assign the 'administrator' role to the user
if ($user instanceof WP_User) {
$user->set_role('administrator');
}
}
}
- Visit your website.
- Log in to WordPress Dashboard.
- Remove the code from functions.php
See Function Reference wp create user – WordPress Codex