Last active
June 17, 2021 06:17
-
-
Save dariodev/a52d0b84fb28f6c778eeffe6e2b7a473 to your computer and use it in GitHub Desktop.
Programmatically Create a User in WordPress
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 | |
// Programmatically Create a User in WordPress | |
add_action('init', 'prefix_add_user'); | |
function prefix_add_user() { | |
$username = 'username123'; | |
$password = 'pasword123'; | |
$email = 'drew@example.com'; | |
$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
Hi,
i called the function in my custom action.When the action do it shows
"Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /var/www/html/project/wordpress/woocommerce-plugin/wp-includes/class-wp-hook.php on line 298".
but the user was created.
can you help me please?