Skip to content

Instantly share code, notes, and snippets.

@junaidtk
Created November 13, 2018 17:55
Show Gist options
  • Save junaidtk/2ec588cca4a488ee48f83295bc362ea1 to your computer and use it in GitHub Desktop.
Save junaidtk/2ec588cca4a488ee48f83295bc362ea1 to your computer and use it in GitHub Desktop.
How to add new admin user through the code snipppets
We can add new admin user by using below code. it will be helpfull if we forget the admin details of the website.
add_action('init', 'add_new_admin_user');
function add_new_admin_user() {
$username = 'testuser';
$email = '[email protected]';
$password = 'password';
$user_id = username_exists( $username );
if ( !$user_id && email_exists($email) == false ) {
$user_id = wp_create_user( $username, $password, $email );
if( !is_wp_error($user_id) ) {
$user = get_user_by( 'id', $user_id );
$user->set_role( 'administrator' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment