Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-user-information.php Secret
Last active Mar 5, 2017

Embed
What would you like to do?
[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
You can’t perform that action at this time.