How to Add an Admin User in WordPress with code. - WPTRINITY

How to Add an Admin User in WordPress with code.

If you have forgotten your WordPress backend password and email, then you can add an admin user by adding this code to your theme’s functions file using an FTP client.

function wpe_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = 'admin@domain.com';
if ( !username_exists( $user )  && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','wpe_admin_account');

Don’t forget to fill in the username, password, and email fields.
After login to your WordPress site, don’t forget to delete the code from your functions file