Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active March 5, 2017 23:57
Show Gist options
  • Save tommcfarlin/d0b38a220778882f23f9de7a69c420e6 to your computer and use it in GitHub Desktop.
Save tommcfarlin/d0b38a220778882f23f9de7a69c420e6 to your computer and use it in GitHub Desktop.
[WordPress] Programmatically Creating WordPress Users
<?php
$user_info = array(
'email' => 'meghan@emaildomain.com',
'first_name' => 'Meghan',
'last_name' => 'McFarlin',
);
<?php
// Read the email address - if it's - invalid or the user exists, then return.
$email = $user_info['email'];
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) || username_exists( $email ) ) {
return;
}
<?php
$password = wp_generate_password( 16, false );
<?php
// Grab the email and create a password.
$email = $user_info['email'];
$password = wp_generate_password( 12, false );
// Create the user and set her role.
$user_id = wp_create_user( $email, $password, $email );
// Set the user's role (and remove the previous role).
$user = new \WP_User( $user_id );
$user->set_role( 'author' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment