How to create a new WordPress user via function.php
Creating a new WordPress user via functions.php
can be an efficient way to manage user accounts, especially if you need to automate tasks or add users programmatically. While you can always add new users through the WordPress dashboard, there are times when using a code-based solution can save time—especially for developers setting up sites, creating demo users, or managing multiple WordPress environments.
In this article, we’ll walk you through how to create a new WordPress user using the functions.php
file in your WordPress theme. By the end, you'll understand the steps involved and how to safely implement this method.
STEPS for adding WordPress user via function.php
1. Open Functions.php file of your Active theme
2. Copy the below code and paste into function.php file in the theme folder
function wordpress_admin_user_account(){
$user = 'Your Username';
$pass = 'Your Password';
$email = 'youremail@xyz.com';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' ); //Set User Role like customer, administrator, subscriber, editor, contributor, author
}
}add_action('init','wordpress_admin_user_account');
3. Open Login Page and login as per the above details
Conclusion
Creating a WordPress user via functions.php
is a powerful technique, especially for developers who need to automate the process or handle large-scale user creation. While it requires careful code handling, it offers flexibility and saves time. Always remember to remove or deactivate your code after use to avoid unnecessary complications.
Want to Hire a Dedicated WordPress Developer for your Website? Click Here!
Leave a Reply