Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

Register WordPress Admin role User through Script

If you are looking for registere Administrator role user in your WordPress website and you don’t have wp-admin access but have File access then you can create – register Admin Role user via Php script and WordPress init hook.

Modify the below code with your email, username, and password and Simply add in your active theme functions.php


add_action('init', 'wdo_register_admin_user');
function wdo_register_admin_user() {
$username = 'wdouser';
$email = 'wdo@gmail.com';
$password = 'wdo@pass$#@123';

$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' );
}
}
}

To execute this code, it’s required once you visit your website and on website load, this code will be executed on WordPress init action and new user with Administrator Role will be created for your use Email id and Password.

Exit mobile version