Skip to content

Instantly share code, notes, and snippets.

@mckernanin mckernanin/functions.php
Last active Mar 8, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.