Create Admin User Programmatically
January 27, 2025 · AI Conversations
### Example Code
Add this to your Laravel project (e.g., in a migration, seeder, or directly through a controller/script):
“`php
use AppModelsAdmin;
use
Here's how you can do it:
### Example Code
Add the following code snippet to your theme's `functions.php` file or as part of a custom plugin:
function create_admin_user_programmatically() {
$username = 'newadmin'; // Desired username
$password = 'strongpassword123'; // Desired password
$email = 'admin@example.com'; // Admin email address
// Check if the user already exists
if (!username_exists($username) && !email_exists($email)) {
// Create the user
$user_id = wp_create_user($username, $password, $email);
if (!is_wp_error($user_id)) {
// Assign the "administrator" role to the user
$user = new WP_User($user_id);
$user->set_role('administrator');
// Optional: Log success in debug
error_log("Admin user '{$username}' created successfully.");
} else {
// Log error if user creation fails
error_log('Failed to create admin user: ' . $user_id->get_error_message());
}
} else {
// Log if the user already exists
error_log('Admin user already exists.');
}
}
// Run the function once
add_action('init', 'create_admin_user_programmatically');
### Important Notes:
1. **Remove or Comment Out the Code**: After the admin user has been created, remove or comment out this code to avoid creating duplicate users or introducing vulnerabilities.
2. **Strong Password**: Always use a strong password to secure the admin account.
3. **Logging**: The `error_log()` lines are optional but useful for debugging.
4. **Testing Environment**: Test this in a staging or local environment before deploying it to production.
Let me know if you'd like help testing or deploying this!
### Example Code
Add the following code snippet to your theme's `functions.php` file, a custom plugin, or run it in a safe environment like WP-CLI:
function assign_admin_role_by_user_id($user_id) {
// Get the user object by ID
$user = new WP_User($user_id);
if ($user) {
// Assign the "administrator" role
$user->set_role('administrator');
// Optional: Log success
error_log("User ID {$user_id} has been assigned the 'administrator' role.");
} else {
// Log error if the user does not exist
error_log("User with ID {$user_id} does not exist.");
}
}
// Replace with the actual user ID
assign_admin_role_by_user_id(123);
### Steps:
1. Replace `123` with the user ID of the account you want to assign the "administrator" role to.
2. Save and execute this code.
– You can temporarily add this to your `functions.php` file and visit any page to trigger it.
– Alternatively, execute it in WP-CLI or a custom admin dashboard script for safety.
### Important Notes:
– **Check for Existing Roles**: If the user already has roles, this will override them. Use `$user->add_role('administrator')` instead of `set_role()` if you want to add the role without removing others.
– **Remove Code After Execution**: Once you've assigned the role, remove the code to avoid leaving unnecessary logic in your WordPress environment.
– **Testing**: Always test in a staging environment first if you're running this on a live site.
Let me know if you need help with this!