-
-
Save ThomasDK81/e7f809077b73451d6e8847c863a2670f to your computer and use it in GitHub Desktop.
Programmatically Create a User in WordPress
This file contains hidden or 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 | |
// Programmatically Create a User in WordPress | |
add_action('init', 'prefix_add_user'); | |
function prefix_add_user() { | |
$username = 'username123'; | |
$password = 'pasword123'; | |
$email = '[email protected]'; | |
$user = get_user_by( 'email', $email ); | |
if( ! $user ) { | |
// Create the new user | |
$user_id = wp_create_user( $username, $password, $email ); | |
if( is_wp_error( $user_id ) ) { | |
// examine the error message | |
echo( "Error: " . $user_id->get_error_message() ); | |
exit; | |
} | |
// Get current user object | |
$user = get_user_by( 'id', $user_id ); | |
} | |
// Remove role | |
$user->remove_role( 'subscriber' ); | |
// Add role | |
$user->add_role( 'administrator' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment