How I add an admin in WordPress in functions.php without being an admin

You can add an admin user in WordPress without being an admin. All you need to do is to add the below PHP code inside your theme’s functions.php file.

Firstly,  you need to insert the code inside the functions.php file found in  your website themes folder. You can upload the code via FTP or by accessing the Web Server directly via Cpanel.

The file functions.php is located in the following path:

You will need to edit the lines 2,3,4 of the below code with your new admin credentials. Change and save.

function wpb_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = '';
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' );
} }

The new user is now created. Now, I recommend to delete this code from your functions.php for security reasons.

Finally, you can login with your new admin credentials via your website login url:

Good Luck!

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *