-
-
Save lucanos/5a051bae5f9fcd5a5141a4f59cfd7f90 to your computer and use it in GitHub Desktop.
Create WordPress Admin User from PHP
This file contains hidden or 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 NEW ADMIN USER TO WORDPRESS | |
// ---------------------------------- | |
// Put this file in your Wordpress root directory and run it from your browser. | |
$user = false; | |
$errors = array(); | |
if( isset( $_POST ) ){ | |
$_POST = array_map("trim", $_POST); | |
if( !isset( $_POST['username'] ) ){ | |
$errors['username'] = 'Username not set'; | |
}elseif( !$_POST['username'] ){ | |
$errors['username'] = 'Blank Username'; | |
}else{ | |
$user['username'] = $_POST['username']; | |
} | |
if( !isset( $_POST['email'] ) ){ | |
$errors['email'] = 'Email not set'; | |
}elseif( !$_POST['email'] ){ | |
$errors['email'] = 'Blank Email'; | |
}elseif( !filter_var( $_POST['email'] , FILTER_VALIDATE_EMAIL ) ){ | |
$errors['email'] = 'Invalid Email'; | |
}else{ | |
$user['email'] = $_POST['email']; | |
} | |
if( !isset( $_POST['password'] ) ){ | |
$errors['password'] = 'Password not set'; | |
}elseif( !$_POST['password'] ){ | |
$errors['password'] = 'Password Email'; | |
}elseif( strlen( $_POST['password'] )<8 || !preg_match( '/[A-Z]/' , $_POST['password'] ) || !preg_match( '/[a-z]/' , $_POST['password'] ) || !preg_match( '/\d/' , $_POST['password'] ) || !preg_match( '/\W/' , $_POST['password'] ) ){ | |
$errors['password'] = 'Weak Password - Requires at least 8 characters including one or more upper, lower, digit and other'; | |
}else{ | |
$user['password'] = $_POST['password']; | |
} | |
} | |
if( $user && !$errors ){ | |
require_once('wp-blog-header.php'); | |
require_once('wp-includes/registration.php'); | |
// Check that user doesn't already exist | |
if( username_exists( $user['username'] ) ){ | |
$errors['username'] = 'Username already in use'; | |
} | |
if( email_exists( $user['email'] ) ){ | |
$errors['email'] = 'Email already in use'; | |
} | |
if( !$errors ){ | |
// Create user and set role to administrator | |
$user_id = wp_create_user( $user['username'] , $user['password'] , $user['email'] ); | |
if( !is_int( $user_id ) ){ | |
$errors['general'] = 'Error with wp_insert_user. No users were created.'; | |
} | |
$wp_user_object = new WP_User( $user_id ); | |
$wp_user_object->set_role( 'administrator' ); | |
unlink(__FILE__); | |
} | |
} | |
if( $user && !$errors ){ | |
?> | |
<h1>User successfully created<h1> | |
<p>This file will now be automatically deleted, and you will be redirected to the <a href="./wp-admin/">Login Page</a> in 5 seconds.</p> | |
<script> | |
setTimeout(function(){document.location.href="./wp-admin/";},5000); | |
</script> | |
<?php | |
die(); | |
} | |
if( !$user || $errors ){ | |
?> | |
<?php echo ( isset( $errors['general'] ) '<p>'.$errors['general'].'</p>' : '' ); ?> | |
<form method="post"> | |
<table> | |
<tr><th>Username</th><td><input type="text" name="username" /><?php if( isset( $errors['username'] ) ? '<br>'.$errors['username'] : '' ); ?></td></tr> | |
<tr><th>Email</th><td><input type="email" name="email" /><?php if( isset( $errors['email'] ) ? '<br>'.$errors['email'] : '' ); ?></td></tr> | |
<tr><th>Password</th><td><input type="password" name="password" /><?php if( isset( $errors['password'] ) ? '<br>'.$errors['password'] : '' ); ?></td></tr> | |
<tr><th colspan="2"><input type="submit" value="Create Admin User" /></th></tr> | |
</table> | |
</form> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment