Create a WordPress Admin User via functions.php or WP-CLI (Safe Method)

create a wordpress admin user via functions.php

Quick answer: To create a WordPress admin user when you are locked out of wp-admin but still have FTP, file manager or SSH access, use WP-CLI or a short functions.php snippet. The safest options are WP-CLI or a one-time snippet that sends a password-reset email instead of storing a password in your code. Remove the snippet as soon as you can log in.

Create a WordPress admin user via functions.php or WP-CLI

Security warning: Code that silently creates admin users is a common malware backdoor. Only use this on a site you own, delete it right after use, and if you ever find code like this in a functions.php you did not add, treat the site as hacked: review all users, change every admin password and scan your theme and plugin files.

If your host gives you SSH access, run this from the WordPress folder (see the wp user create documentation). WordPress emails the new user a link to set their password.

terminal
wp user create your-username you@yourdomain.com --role=administrator --send-email

Option 2: Create a WordPress admin user via functions.php (one-time snippet)

Connect with FTP or your host’s file manager, open wp-content/themes/your-theme/functions.php, paste the code at the end and change the username and email. Load any page on your site once, check your inbox for the set-password email, log in, then delete the code.

functions.php
/**
 * Emergency admin account - runs ONCE, then remove this code.
 * No password is stored here: WordPress emails a "set your password" link.
 */
add_action( 'init', function () {
    if ( get_option( 'tdev_emergency_admin_done' ) ) {
        return;
    }

    $username = 'your-username';     // change this
    $email    = 'you@yourdomain.com'; // an inbox you control

    if ( username_exists( $username ) || email_exists( $email ) ) {
        update_option( 'tdev_emergency_admin_done', 1 );
        return;
    }

    $user_id = wp_insert_user( array(
        'user_login' => $username,
        'user_email' => $email,
        'user_pass'  => wp_generate_password( 24 ),
        'role'       => 'administrator',
    ) );

    if ( ! is_wp_error( $user_id ) ) {
        wp_new_user_notification( $user_id, null, 'user' );
        update_option( 'tdev_emergency_admin_done', 1 );
    }
} );

Why this version is safer than the usual snippet

Most snippets online hard-code a plain-text password and run on every page load forever. This version stores no password, runs only once, checks for errors and uses an email link so only the owner of that inbox can set the password.

After you regain access

  • Delete the snippet from functions.php.
  • Remove any admin accounts you do not recognise.
  • Update WordPress, your theme and plugins.
  • Use a strong, unique password and turn on two-factor login.

Locked out and not comfortable editing files? Contact me and I will restore your access safely.

Frequently asked questions

How do I add an admin account with functions.php?

Add a one-time snippet that calls wp_insert_user() with the administrator role and sends a set-password email, load the site once, log in, then delete the snippet.

Is it safe to add an admin user with code?

Only on your own site and only temporarily. Never hard-code a password, and remove the code right after you log in. Unknown code like this in functions.php is a sign of a hacked site.

How do I create an admin user with WP-CLI?

Run: wp user create username you@example.com –role=administrator –send-email from the WordPress folder over SSH.

Need help with your own site? Get a free quote or see my recent builds.

Tanvir Hossain Tofan, WordPress developer

Written by Tanvir Hossain Tofan

WordPress & Shopify developer from Bangladesh with 5+ years of experience building custom ACF themes, Elementor sites, plugins and Shopify stores for clients worldwide. Author of the Toofan Stage-to-Live Sync and Talkvara AI Chatbot plugins on WordPress.org.

See my work · Hire me · WhatsApp · YouTube · LinkedIn

Previous Post

jQuery Tabs Next Prev Button: Step-by-Step Form with Bootstrap Tabs

Add Next and Previous buttons to Bootstrap tabs wi ...

Next Post

Block Email Domains in Fluent Forms: 2 Easy PHP Snippets

Block email domains in Fluent Forms on all forms o ...

If you find it useful

Buy me a coffee

ACF

Blog

Elementor

HTML JQuery

PHP

Tips & Tricks

WordPress

Shopify vs WooCommerce: Which Is Better for Your Online Store in 2026?

Shopify or WooCommerce? Real costs, payments in Ba ...

WordPress Website Redesign: 7 Signs You Need One and What It Costs

Seven signs you need a WordPress website redesign, ...

bKash Payment Gateway in WooCommerce: How to Add bKash, Nagad and Cards

How to add a bKash payment gateway to WooCommerce: ...

Restaurant Online Ordering System: How to Set Up Your Own Without Commission

How to set up your own restaurant online ordering ...

WordPress Speed Optimization: How I Took a Site From 30 to 99 on PageSpeed

How I took a WordPress site from 30 to 99 on mobil ...

Back to top