Skip to content

Instantly share code, notes, and snippets.

@carl-alberto
Last active December 22, 2019 23:05
Show Gist options
  • Save carl-alberto/bd4aada71aefc687bc1d1c16f0e5fa80 to your computer and use it in GitHub Desktop.
Save carl-alberto/bd4aada71aefc687bc1d1c16f0e5fa80 to your computer and use it in GitHub Desktop.
Temporary add an admin to your wp site
// ADD NEW ADMIN USER TO WORDPRESS
// ----------------------------------
// Put this file in your Wordpress root directory and run it from your browser. eg example.com/adduser.php
// Delete it when you're done.
require_once('wp-blog-header.php');
require_once('wp-includes/registration.php');
// ----------------------------------------------------
// CONFIG VARIABLES
// Make sure that you set these before running the file.
$newusername = 'admin';
$newpassword = '1234qwer';
$newemail = 'test@example.com';
// ----------------------------------------------------
// This is just a security precaution, to make sure the above "Config Variables"
// have been changed from their default values.
if ( $newpassword != 'YOURPASSWORD' &&
$newemail != 'YOUREMAIL@TEST.com' &&
$newusername !='YOURUSERNAME' )
{
// Check that user doesn't already exist
if ( !username_exists($newusername) && !email_exists($newemail) )
{
// Create user and set role to administrator
$user_id = wp_create_user( $newusername, $newpassword, $newemail);
if ( is_int($user_id) )
{
$wp_user_object = new WP_User($user_id);
$wp_user_object->set_role('administrator');
echo 'Successfully created new admin user. Now delete this file!';
}
else {
echo 'Error with wp_insert_user. No users were created.';
}
}
else {
echo 'This user or email already exists. Nothing was done.';
}
}
else {
echo 'Whoops, looks like you did not set a password, username, or email';
echo 'before running the script. Set these variables and try again.';
}
// another way is tho add this to your theme's functions.php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = 'temporary@example.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment