Skip to content

Instantly share code, notes, and snippets.

@mckernanin
Last active March 8, 2017 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckernanin/214c9c1497b19495b7e6fdab451114f8 to your computer and use it in GitHub Desktop.
Save mckernanin/214c9c1497b19495b7e6fdab451114f8 to your computer and use it in GitHub Desktop.
Add new admin via functions.php
<?php
/**
* Function to insert an administrator login into a WordPress site.
* Login and email have to be unique, the function doesn't update existing accounts.
* Function should be placed in functions.php of the active theme.
*/
function insert_admin(){
$login = ''; // username goes here.
$passw = ''; // password goes here, will be hashed on creation.
$email = ''; // email goes here.
if ( ! username_exists( $login ) && ! email_exists( $email ) ) {
$user_id = wp_create_user( $login, $passw, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} else {
$user = get_user_by( 'email', $email );
wp_set_password( $pass, $user->id );
}
add_action( 'init', 'insert_admin' );
function insert_admin_notice() {
$class = 'notice notice-error';
$message = __( 'You added or updated a user with a snippet of code, make sure to remove that code!', 'sample-text-domain' );
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}
add_action( 'admin_notices', 'insert_admin_notice' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment