Category Archives: Users

Programmatically create an admin user in WordPress

Sometimes you may need to add a new user to the WordPress database via code. Below is a copy and paste snippet… just remember to swap out the username, password and email for your own.

// funcitons.php
/**
 * Create an admin user silently
 */
function my_add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    // Create the new user
    $user_id = wp_create_user( $username, $password, $email );

    // Get current user object
    $user = get_user_by( 'id', $user_id );

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
}
add_action('init', 'my_add_user');