In the world of development, there come moments when you find yourself in need of a new administrative user for your database access, especially when working with exports of platforms like WordPress. Often, you’re handed a WordPress database export but lack the necessary admin user login credentials. While accessing the database is an option, it’s worth noting that this approach can be somewhat intricate for developers less familiar with MySQL. Additionally, the process can be time-consuming. In this article, I’ll guide you through a swifter alternative that doesn’t involve delving into the complexities of MySQL.
Add a WordPress Admin User to Your Database with PHP : A Step-by-Step Guide
Add a WordPress Admin User to Your Database with PHP : A Step-by-Step Guide
To begin, establish a new directory named ‘mu-plugins/’ within the ‘wp-content/’ directory of your website. This new directory will play a crucial role in our streamlined process.
Inside the ‘wp-content/mu-plugins/’ directory, create a fresh PHP file and give it a name that resonates with its purpose. For instance, ‘create-admin-user.php’ would serve as an appropriate choice.
Integrate the Code: Open the newly created PHP file and insert the following code snippet:
<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = '[email protected]';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
} );
Customize User Details:Within the code, there are three variables that require your attention:
$username: Set this to the desired username for your new admin user.
$password: Choose a secure password for the new admin user.
$email_address: Enter a valid email address associated with the admin user.
Access and Cleanup: Save the PHP file after customizing the user details. Now, log in to your website using the credentials you just configured. Once you’ve successfully logged in, it’s essential to promptly remove the PHP file from the ‘mu-plugins/’ directory
Creating a new administrative user to gain swift access to your website doesn’t have to involve navigating complex MySQL operations. By following these simplified steps, you can efficiently establish a new admin user and maintain control over your website’s functionality. This method not only saves you time but also grants you a more intuitive approach to handling administrative access without delving into the intricacies of database management.