• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tips and Tricks HQ

  • Home
  • Blog
  • Projects
    • All Projects
    • Simple WP Shopping Cart
    • WP Express Checkout Plugin
    • WP Download Monitor
    • WP Security and Firewall Plugin
    • WP eStore Plugin
    • WP Affiliate Platform
    • WP eMember
    • WP Lightbox Ultimate
    • WP Photo Seller
  • Products
    • All Products
    • Checkout
  • Support
    • Support Portal
    • Customer Only Forum
    • WP eStore Documentation
    • WP Affiliate Software Documentation
    • WP eMember Documentation
  • Contact

Add a WordPress Admin User Account via PHP

You are here: Home / Wordpress / Add a WordPress Admin User Account via PHP

Last updated: October 18, 2020 by Ruhul Amin


Sometimes our WordPress plugin users need to create an Admin user account for their sites. In this tutorial I will share a small PHP code with you that can be used to create a WordPress Administrator user to your site.

Alternatively, you can also create a WordPress admin user via MySQL.

You will need to have FTP access to your site so you can access and edit the theme files.

Step 1) Log in via FTP

Log into your site via FTP and browse to your theme’s folder. It should be in the following location:



wp-content/themes/

You can use a free software like FileZilla to log into your site using FTP.

Step 2) Add the PHP Code

Copy and paste the following PHP code in your theme’s functions.php file. Change the value of the username, password and email fields.

function create_new_admin_user_account() {
    $username = 'new_admin';
    $password = 'New_Password!123';
    $email = '[email protected]';

    //Check to see if an account exists with the given username and email address value.
    if ( !username_exists($username) && !email_exists($email) ) {
        //All clear. Go ahead.

        //Create WP User entry
        $user_id = wp_create_user($username, $password, $email);

        //WP User object
        $wp_user = new WP_User($user_id);

        //Set the role of this user to administrator.
        $wp_user->set_role('administrator');
    }
}
//Run the function with WordPress's "init" hook is triggered.
add_action('init', 'create_new_admin_user_account');

Step 3) Execute the Code

Access your site’s homepage to execute this code.

You can now log in using this newly created user.

Delete the code from the theme’s functions.php file after the task is done.

Related Posts

  • Create a WordPress Admin User for A WordPress Site via MySQL (PHPMyAdmin)
  • How to import WordPress SQL database backup file without having ‘create new database’ privileges in phpMyAdmin
  • How to Create a WordPress plugin
  • How to Begin Creating a WordPress Plugin

Web Development,  Wordpress creating WordPress plugin,  Site admin tips,  Tech Tips,  Troubleshooting

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Featured & Popular Articles

Video Answers to Top WordPress QuestionsWordPress Optimization Tips and Tricks for Better Performance and SpeedEssential WordPress Security Tips - Is Your Blog Protected?WordPress Simple PayPal Shopping Cart PluginTop 15 Search Engine Optimization (SEO) Techniques I Forget to DoList of the Best and Must Use WordPress PluginsHow do I Start a Blog and Make Money Online?Good Domain Name Picking Tips for Your Blog SetupFind Out Which WordPress Web Hosting Company Offers the Cheapest and Reliable Web Hosting Solution

Featured WordPress Plugins

wordpress estore plugin
wordpress membership plugin
WP Express Checkout Plugin
WordPress Lightbox Ultimate Plugin
WordPress photo seller plugin
wordpress affiliate plugin

Recent Posts

  • Accept Donations via PayPal from Your WordPress Site Easil [...]
  • Buy Now Button Graphics for eCommerce Websites [...]
  • Subscription Button Graphics for eCommerce Websites [...]
  • Adding PayPal Payment Buttons to Your WordPress Sidebar Ea [...]
  • PayPal QR Codes [...]

Comment & Socialize

  • @Rodrigo Souza, Thank you f ...
    - admin
  • The example for 'slm_add_ed ...
    - Rodrigo Souza
  • @Ron, All the valid transac ...
    - admin
  • Hello, when people have sel ...
    - Ron
  • We have hte following featu ...
    - admin

Check out our solutions

View our WordPress plugin collection and start using them on your site.

Our WordPress Solutions

Footer

Company

  • About
  • Privacy Policy
  • Terms and Conditions
  • Affiliate Login

Top WordPress Plugins

  • Simple Shopping Cart
  • PayPal Donations
  • WP Express Checkout
  • WP eStore
  • WP eMember

Blogging Tips

  • How to Start a Blog
  • Selecting a Good Domain
  • Cheap WP Hosting
  • WP Video Tutorials
  • Simple SEO Tips

Search


Keep In Touch

Copyright © 2023 | Tips and Tricks HQ