Created
January 17, 2021 03:51
-
-
Save wpsmith/8e2c0c8a2e71d1b1c4f6bc6ffc4e4802 to your computer and use it in GitHub Desktop.
PHP: WordPress New Admin/Super Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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