Occasionally it happens where we are inheriting a site developed by someone else where we have file system access, but not access to WordPress itself. When this occurs, we can use PHP to create a new WordPress administrator account to let us into WordPress to take full control of the site.
Here is a block of code that can be used to create a new WordPress administrator. Drop this into the functions.php file of the active theme and load the site. In the background it will execute and create a new administrator account with the credentials specified below. Edit the functions.php file again to remove this code so that it doesn’t execute each time the site is accessed.
1 2 3 4 5 6 7 8 9 10 |
add_action( 'init', function () { $username = 'tempadmin'; $password = 'astrongpassword'; $email_address = 'tempadmin@glimmernet.com'; if ( ! username_exists( $username ) ) { $user_id = wp_create_user( $username, $password, $email_address ); $user = new WP_User( $user_id ); $user->set_role( 'administrator' ); } } ); |
Please, use this code only for good! 🙂
Note that if you have a security plugin such as Wordfence, you will receive a notification that a user was created outside of the WordPress installation. This is normal and can be ignored.
0 Comments