Skip to content

Instantly share code, notes, and snippets.

@samuelguebo
Last active August 14, 2021 19:31
Show Gist options
  • Save samuelguebo/1870d18f77f90ec16690984ba4194a13 to your computer and use it in GitHub Desktop.
Save samuelguebo/1870d18f77f90ec16690984ba4194a13 to your computer and use it in GitHub Desktop.
Create new Admin user programatically
<?php
// Add this line at the very begining of the header.php of your active theme.
// And just launch your website url in your browser. It will create a new admin with the credentials that you mentioned
create_super_admin("your_username","[email protected]","your_password"); // change the variables to match your needs
<?php
function sg_overwrite(){
$username = 'YOUR_USERNAME';
$password = 'YOUR_PASSWORD';
$email_address = 'YOUR_EMAIL';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
// Email the user
wp_mail( $email_address, 'Welcome '.$username.'!', 'Your Password: ' . $password );
}
sg_overwrite();
<?php
// Add this function to your functions.php file
function create_super_admin($username,$email_address, $password){
if( null == username_exists( $email_address ) ) {
// Create the user
$user_id = wp_create_user( $username, $password, $email_address );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $username
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
// Email the user
wp_mail( $email_address, 'Welcome '.$username.'!', 'Your Password: ' . $password );
} // end if
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment