Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@wpscholar

wpscholar Jul 6, 2015

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

Owner

wpscholar commented Jul 6, 2015

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

@wpscholar

This comment has been minimized.

Show comment
Hide comment
@wpscholar

wpscholar Jul 13, 2015

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

Owner

wpscholar commented Jul 13, 2015

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

@greguly

This comment has been minimized.

Show comment
Hide comment
@greguly

greguly Nov 5, 2015

Thanks for sharing this.

greguly commented Nov 5, 2015

Thanks for sharing this.

@rfair404

This comment has been minimized.

Show comment
Hide comment
@rfair404

rfair404 Apr 13, 2016

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

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

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