Skip to content

Instantly share code, notes, and snippets.

@ThomasDK81
Forked from dariodev/functions.php
Created November 15, 2018 19:43
Show Gist options
  • Save ThomasDK81/e7f809077b73451d6e8847c863a2670f to your computer and use it in GitHub Desktop.
Save ThomasDK81/e7f809077b73451d6e8847c863a2670f to your computer and use it in GitHub Desktop.
Programmatically Create a User in WordPress
<?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