Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Created May 14, 2019 11:46
Show Gist options
  • Save itsmikita/a64f5a5ea532f5827b4c4189b89c08a8 to your computer and use it in GitHub Desktop.
Save itsmikita/a64f5a5ea532f5827b4c4189b89c08a8 to your computer and use it in GitHub Desktop.
Create new Admin user in WordPress
<?php
/**
* Creates new user with Administrator capabilities
*
* Put this code snippet to your theme's functions.php
* and change $username, $password and $email. The code
* checks if $username does not exist and creates a new
* user.
*
* NOTE: This runs on every page load, so don't forget
* to comment this code out or remove it after you
* create the new user to save load time.
*/
add_action( "init", function() {
$username = "itsmikita";
$password = "passwort";
$email = "[email protected]";
if( ! username_exists( $username ) ) {
$userId = wp_create_user(
$username,
$password,
$email
);
$user = get_user_by( "id", $userId );
$user->remove_role( "subscriber" );
$user->add_role( "administrator" );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment