Last active
February 15, 2019 03:39
-
-
Save shawn-crigger/15574beafa23f7bc516027305b7b76fc to your computer and use it in GitHub Desktop.
Quick function to create a admin user in WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Creates an WP admin. | |
* @see https://tommcfarlin.com/create-a-user-in-wordpress/#code | |
* @see | |
* @param string $email_address The email address | |
* @return boolean | |
*/ | |
function _create_admin( $email_address = '' ) | |
{ | |
if( NULL == username_exists( $email_address ) ) return FALSE; | |
// Generate the password and create the user | |
$password = wp_generate_password( 12, FALSE ); | |
$user_id = wp_create_user( $email_address, $password, $email_address ); | |
if ( is_wp_error( $user_id ) ) return FALSE; | |
// Set the nickname | |
$user_id = wp_update_user( | |
array( | |
'ID' => $user_id, | |
'nickname' => $email_address | |
) | |
); | |
if ( is_wp_error( $user_id ) ) return FALSE; | |
// Set the role | |
$user = new WP_User( $user_id ); | |
$user->set_role( 'administrator' ); | |
// Email the user | |
wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password ); | |
return TRUE; | |
} // end _create_admin | |
_create_admin( '@gmail.com '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment