Skip to content

Instantly share code, notes, and snippets.

Last active December 16, 2023 21:58
Show Gist options
  • Save jawinn/8012327 to your computer and use it in GitHub Desktop.
Save jawinn/8012327 to your computer and use it in GitHub Desktop.
Create WordPress Admin User from PHP
// ----------------------------------
// Put this file in your Wordpress root directory and run it from your browser.
// Delete it when you're done.
// ----------------------------------------------------
// Make sure that you set these before running the file.
$newusername = 'YOURUSERNAME';
$newpassword = 'YOURPASSWORD';
$newemail = '';
// ----------------------------------------------------
// This is just a security precaution, to make sure the above "Config Variables"
// have been changed from their default values.
if ( $newpassword != 'YOURPASSWORD' &&
$newemail != '' &&
$newusername !='YOURUSERNAME' )
// Check that user doesn't already exist
if ( !username_exists($newusername) && !email_exists($newemail) )
// Create user and set role to administrator
$user_id = wp_create_user( $newusername, $newpassword, $newemail);
if ( is_int($user_id) )
$wp_user_object = new WP_User($user_id);
echo 'Successfully created new admin user. Now delete this file!';
else {
echo 'Error with wp_insert_user. No users were created.';
else {
echo 'This user or email already exists. Nothing was done.';
else {
echo 'Whoops, looks like you did not set a password, username, or email';
echo 'before running the script. Set these variables and try again.';
Copy link

ghost commented Sep 19, 2014

This is a really neat script.

Just wondering if it's possible to be run from somewhere other than the root e.g. from the wp-content folder?

Copy link

stefanopascazi commented Sep 10, 2018

require 'wp-load.php';
$username = 'username';
$password = 'password';
$email_address = '';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} ?>`

Copy link

MahbbRah commented Feb 26, 2022

@stefanopascazi Thank you so much, man! This saved my Ass. I mean I get to know loading the WP methods inside a custom PHP script, which helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment