1. Home
  2. How To
  3. WP Hacks

[How to] Add a User to WordPress Programmatically

How2Buy // (Updated: )

In a recent development project, I had to add a user to WordPress programmatically because I had no WP user data but only access to FTP. If this is your case, here is the code I used to add myself to the WordPress users:

/* Add user to wordpress programmatically */
$user_name = "wpadmin";
$user_email = "[email protected]";
$user_pass = "1234";
$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
    $random_password = $user_pass; // Alternate : wp_generate_password( $length=5, $include_standard_special_chars=false );
    $user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
    $random_password = __('User already exists.  Password inherited.');
print $random_password;

No Comments

Leave a Reply

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

Share Share Tweet