How to create new user to WordPress with PHP?

Have you had situations where you have access to project via FTP but everybody have forgot administrator credentials. So it is possible to make new user via phpMyadmin or with PHP script what is possible to upload to server via FTP.

This script should be saved as php file info WordPress root folder (with .php extension)

  1. Make file and copy code from below.
  2. Update config part with your new user data (newusername, newpassword and newemail)
  3. Save file into WordPress root folder.
  4. Open this file directly in your web browser.
  5. When new user is created, delete this newly created file.
  6. Log into admin panel with this new user
require_once ('wp-blog-header.php');
require_once ('wp-includes/registration.php');
// Config
$newusername = 'username';
$newpassword = 'password';
$newemail = '';
// Make sure that you updated config data
if ($newpassword != 'password' && $newemail != '' && $newusername != 'username') {
    // Control is user don't exist already
    if (!username_exists($newusername) && !email_exists($newemail)) {
        // Create new user and give administrator access
        $user_id = wp_create_user($newusername, $newpassword, $newemail);
        if (is_int($user_id)) {
            $wp_user_object = new WP_User($user_id);
            echo 'New user is succesfuly created. Delete this file now!';
        } else {
            echo 'Problem with function wp_insert_user. New user was not created.';
    } else {
        echo 'This user already exists!';
} else {
    echo 'Please update Config eg. username, password and e-mail with your own data. ';
    echo 'Update those and try again.';

Leave a Reply