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.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

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 = '';
	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' );

Code language: PHP (php)

Donโ€™t forget to replace temp_admin, p4ssw0rd!, and 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.

That’s it! 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.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

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