How to Add an Admin User to WordPress via functions.php
Adding an admin user to your WordPress website can be helpful in many scenarios, such as when you need to grant someone access to your website or when you need to create a new administrator account. By default, adding an admin user requires logging into the WordPress dashboard and navigating to the user management area. However, you can also add an admin user to WordPress via the functions.php file. This tutorial provides a step-by-step guide on how to add an admin user to your WordPress website via the functions.php
file.
Access the functions.php file: Log in to your WordPress dashboard, go to Appearance > Theme Editor, and select the functions.php file from the right-side list. Add the following code snippet:
function create_admin_user_fn() {
$username = 'temp_admin';
$password = 'p4ssw0rd!';
$email_address = 'email@hoolite.be';
if ( ! username_exists( $username ) && !email_exists( $email_address ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}
add_action( 'init', 'create_admin_user_fn' );
Donβt forget to replace temp_admin
, p4ssw0rd!
, and email@hoolite.be
with your own values.
After adding the code, save the functions.php
file. To verify that the new user was successfully added, log out of the WordPress dashboard and log back in with the new admin user’s username and password. You can also navigate to Users in the WordPress dashboard to verify that the new user was added.
Note: As with editing any code in the functions.php file, it’s crucial to be cautious, as a small mistake in the code can result in your website not functioning correctly. To avoid any issues, it’s recommended to make a backup of the functions.php
file before making any changes.
In conclusion, adding an admin user to WordPress via the functions.php
file is a simple process that can save you time and effort. By following this step-by-step guide, you can easily grant access to your WordPress website to a new admin user without logging into the WordPress dashboard. Remember to make a backup of the functions.php
file before making any changes to minimize the risk of any issues.
That’s it!
Support πΆ
If you found this article helpful, got a question or spotted an error/typo... Do well to leave your feedback in the comment section or help spread it by sharing this article. If you're feeling generous (and I hope you do) you can definitely help me by getting me a cup of coffee β.
Leave a Reply