-
-
Save tommcfarlin/d0b38a220778882f23f9de7a69c420e6 to your computer and use it in GitHub Desktop.
[WordPress] Programmatically Creating WordPress Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user_info = array( | |
'email' => 'meghan@emaildomain.com', | |
'first_name' => 'Meghan', | |
'last_name' => 'McFarlin', | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$password = wp_generate_password( 16, false ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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