This script loads WordPress, checks if the user exists, and if not, creates a new administrator account with a randomly generated password.
<?php
// Script to create an administrator user using the credentials from wp-config.php
// Load WordPress
require_once __DIR__ . '/wp-load.php';
// New user data
$username = 'jsuarez'; // change as needed
$email = '[email protected]'; // change as needed
// Generate random password
$password = wp_generate_password(20, true, true);
// Check if the username or email already exists
if (username_exists($username) || email_exists($email)) {
echo "The user already exists. No changes were made.";
exit;
}
// Create user
$user_id = wp_create_user($username, $password, $email);
if (is_wp_error($user_id)) {
echo "Error creating the user: " . $user_id->get_error_message();
exit;
}
// Assign administrator role
$wp_user = new WP_User($user_id);
$wp_user->set_role('administrator');
echo "Administrator user created successfully.\n";
echo "Username: $username\n";
echo "Email: $email\n";
echo "Generated password: $password\n";
?>