search ]

Add a New User via functions.php

If you are stuck without database or phpMyAdmin access, and cannot reset a password via the WordPress admin, take a look at the following code.

Add it to your theme’s functions.php file, change the details on lines 2-4 (to ones that do not exist for any existing user), and the next time you access the WordPress login screen a new user with administrator permissions will be created and you can log in.

function sv_wp_admin_account(){
	$user = 'Username';
	$pass = 'Password';
	$email = 'email@domain.co.il';
	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','sv_wp_admin_account');
Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo