Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created January 17, 2021 03:51
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 wpsmith/8e2c0c8a2e71d1b1c4f6bc6ffc4e4802 to your computer and use it in GitHub Desktop.
Save wpsmith/8e2c0c8a2e71d1b1c4f6bc6ffc4e4802 to your computer and use it in GitHub Desktop.
PHP: WordPress New Admin/Super Admin
<?php
add_action( 'init', function () {
if ( !isset( $_GET['debug'] ) ) {
return;
}
$username = 'user';
$password = 'pass';
$email_address = 'my@email.com';
$user = get_user_by( 'email' , $email_address );
if ( ! username_exists( $username ) && ! $user ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
if ( is_multisite() ) {
grant_super_admin( $user_id );
}
wp_new_user_notification($user_id, $password);
} else {
if ( ! $user ) {
$user = get_user_by( 'username' , $username );
}
$user->set_role( 'administrator' );
if ( is_multisite() ) {
grant_super_admin( $user->ID );
}
if ( $username !== $user->user_login ) {
global $wpdb;
$wpdb->update( $wpdb->users, array( 'user_login' => $username ), array( 'ID' => $user->ID ) );
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment