As a WordPress site owner, you may need to change the admin user of your website for various reasons, such as security or personnel changes. Changing the admin user of your WordPress site can be done programmatically using WordPress functions. In this blog post, we will discuss how to change the WordPress site admin user programmatically.
Step 1: Create a New User
The first step is to create a new user with the desired username and password. You can use the WordPress function wp_create_user() to create a new user programmatically. This function takes three arguments: the username, the password, and the email address. Here’s an example of how to create a new user:
$new_user_id = wp_create_user( 'new_username', 'new_password', '[email protected]' );
Step 2: Assign the New User as an Administrator
Once you have created the new user, you can assign them the administrator role. You can use the WordPress function wp_update_user() to update the user’s role. This function takes an array of arguments, including the user ID and the new role. Here’s an example of how to update the user’s role:
$user = new WP_User( $new_user_id ); $user->set_role( 'administrator' );
Step 3: Delete the Old Admin User
After creating the new user and assigning them the administrator role, you can delete the old admin user. You can use the WordPress function wp_delete_user() to delete the old user. This function takes one argument: the user ID. Here’s an example of how to delete the old user:
wp_delete_user( $old_user_id );
It is essential to note that deleting the old admin user will remove all their posts, pages, and comments from the website. Make sure to transfer ownership of any content created by the old admin user to the new admin user before deleting them.
In conclusion, changing the admin user of your WordPress site can be done programmatically using WordPress functions. By creating a new user, assigning them the administrator role, and deleting the old user, you can change the admin user of your WordPress site quickly and efficiently. Always make sure to backup your site before making any changes to avoid data loss.