If you’re locked out of your WordPress admin or need to create a new administrator account manually, there are two reliable methods: using functions.php or directly via the database.
This guide covers both approaches step by step.
Method 1: Create Admin User via functions.php
This is the easiest method if you have access to your theme files.
Steps:
- Open your active theme’s
functions.phpfile - Add the following code (update credentials before saving)
function create_new_admin_account() {
$username = 'newadmin';
$password = 'StrongPassword123!';
$email = 'youremail@example.com';
if ( !username_exists($username) && !email_exists($email) ) {
$user_id = wp_create_user($username, $password, $email);
$user = new WP_User($user_id);
$user->set_role('administrator');
}
}
add_action('init', 'create_new_admin_account');Important:
- Visit your website once after adding the code
- The admin user will be created automatically
- Remove the code immediately after execution to prevent security risks
Method 2: Create Admin User via Database
Use this method if you don’t have access to WordPress files but can access the database (via phpMyAdmin).
Step 1: Insert User
INSERT INTO wp_users
(user_login, user_pass, user_nicename, user_email, user_status, display_name)
VALUES
('newadmin', MD5('StrongPassword123!'), 'newadmin', 'youremail@example.com', 0, 'newadmin');Step 2: Get User ID
After inserting, note the ID of the new user (e.g., 7).
Step 3: Assign Administrator Role
INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
VALUES (7, 'wp_capabilities', 'a:1:{s:13:"administrator";b:1;}');
INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
VALUES (7, 'wp_user_level', '10');Note: Replace 7 with your actual user ID and update the table prefix if it’s not wp_.
Conclusion
Both methods are effective for regaining admin access:
- functions.php: Quick and easy if you have file access
- Database method: Useful when locked out completely
Security Tip: After creating the admin account, always:
- Remove any temporary code
- Use a strong password
- Delete unused or suspicious users