Skip to content

Instantly share code, notes, and snippets.

@jesussuarz
Created December 5, 2025 15:07
Show Gist options
  • Select an option

  • Save jesussuarz/1d18697fafff567efb29c23cdf0b85b8 to your computer and use it in GitHub Desktop.

Select an option

Save jesussuarz/1d18697fafff567efb29c23cdf0b85b8 to your computer and use it in GitHub Desktop.
Create New Admin Wordpress Script PHP

Admin User Auto-Creation Script for WordPress

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";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment