Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active February 27, 2024 11:54
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save wpscholar/86c236469f3610400f8a to your computer and use it in GitHub Desktop.
Save wpscholar/86c236469f3610400f8a to your computer and use it in GitHub Desktop.
Create a new admin user in WordPress via code. Drop this file in the mu-plugins directory and update the variables, then load a page in WordPress to create the user. Remove the file when done.
<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = 'webmaster@mydomain.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
} );
@wpscholar
Copy link
Author

To send an email to the new user, just add this below line 12:
wp_new_user_notification($user_id, $password);

@wpscholar
Copy link
Author

On multisite, just do this to grant super admin privileges: grant_super_admin( $user_id );

@greguly
Copy link

greguly commented Nov 5, 2015

Thanks for sharing this.

@rfair404
Copy link

I knew this would come in hand one day, again...

@WordsofDefiance
Copy link

Would this user still show up in the regular user list for all other admins?

@mircobabini
Copy link

Would this user still show up in the regular user list for all other admins?

Yes, of course.

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