TL;DR copy the below code into a file and upload it here, creating the new folder.
wp-content/mu-plugins/create-user.php
Refresh the website and remove the file after you’ve successfully logged in.


if your admin user is deleted use this code for create new admin user.
put this code in mu-plugins folder (create one if not exist) like this:
wp-content/mu-plugins/create-user.php
then refresh your site . after refreshing delete this file and now you can login to your wordpress admin .

Time needed: 10 minutes

To regain access to your WordPress admin area after the admin user has been deleted, you can follow these steps:

  1. Create a New Admin User

    If you’ve lost access to the admin user account, you can create a new admin user by adding a code snippet to your WordPress installation. This snippet will create a new admin user for you to use.

  2. Placing the Code Snippet

    Place the code snippet in a file named create-user.php. You can do this by creating a new .txt file, adding the below code, and renaming it. If you can’t see file extensions you can change this in windows folder options. Save it within the mu-plugins directory. If this directory doesn’t exist, you can create it within the wp-content directory. The file path should be:bashCopy codewp-content/mu-plugins/create-user.php

  3. Upload the File and Refresh

    Once you’ve placed the create-user.php file in the mu-plugins directory, refresh your website. This will execute the code and create a new admin user.

  4. Remove the Code File

    After you’ve successfully logged in using the new admin user, delete the create-user.php file from the mu-plugins directory. This step ensures that the code snippet isn’t accessible anymore, enhancing the security of your site.

By following these steps, you’ll be able to create a new admin user, regain access to your WordPress admin area, and ensure that your site remains secure by removing the temporary code file.

Remember to take appropriate security precautions, such as using strong passwords and regularly updating your plugins and themes, to prevent unauthorized access to your WordPress site in the future.

add_action( 'init', function () {

	$username = 'admin';
	$password = 'admin';
	$email    = 'admin@dev.io';

	if ( username_exists( $username ) ) {
		wp_die( 'Username Exists!' );
	}

	$user = wp_create_user( $username, $password, $email );

	if ( is_wp_error( $user ) ) {
		wp_die( $user->get_error_message() );
	}

	$user = get_user_by( 'id', $user );
	$user->add_cap( 'administrator' );

}, 999 );

Answer is inspired by this answer, this really saved me. 💕

Home » Blog » Uncategorised » All users / admin deleted in WordPress
Categories: Uncategorised

David

David has a proven track record in digital marketing, web development, graphic design and video production. He has managed high-impact projects for global brands like Rentokil Initial and Microsoft UK, while also demonstrating expertise in Python programming and AI. David's passion for technology, coupled with his proficiency in social media strategy and creative problem-solving, make him an ideal candidate to tackle diverse challenges aligned with his values.