Create an Admin User in WordPress :

In case your WordPress website was hacked, you forgot your password, or for some motive, you can’t log in to your WordPress admin dashboard, do not despair. There’s one other solution to acquire access to your user account – it requires creating a new WordPress admin user by directly modifying your site’s database table.

In this article, we’ll show you how to add an admin user to the WordPress database via SQL Query and write a code in function.php, so you can get your access back.

Create an Admin User in WordPress via functions.php

All through this article, we look into how to use the wp_create_user() and wp_insert_user() functions. As well as, we’ll compare them and discover out where to make use of every WordPress user function to create WooCommerce clients as well.
You may all the time create users by using the WordPress dashboard. However this strategy just isn’t sufficient in case you need to programmatically create WordPress users. In these instances, we need to use some PHP code.

There are many reasons why you may need to create a WordPress user programmatically. case you are importing user data from one other system, and also you need to create new users to your WordPress website, you might need to create WordPress users programmatically. On this tutorial, you’ll learn how to add new users to your website in an automatic way.

function add_new_user() {
    $username = 'demouser';
    $email = '';
    $password = 'Pasword@123';

    $user_id = username_exists( $username );
    if ( !$user_id && email_exists($email) == false ) {
        $user_id = wp_create_user( $username, $password, $email );
        if( !is_wp_error($user_id) ) {
            $user = get_user_by( 'id', $user_id );
            $user->set_role( 'administrator' );
add_action('init', 'add_new_user');
create admin by functions.php in wordpress create an admin user in WordPress

Also Read : How to Create Custom Post Types in WordPress Without Plugins

Create an Admin User in WordPress Database Using a SQL Query

If you are a developer, then you possibly can speed up the process using code. Simply drop this SQL query into your database.

INSERT INTO `newsportal_db`.`wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES ('3', 'Pass@#4', MD5('Pass@#4'), 'Your Name', '', '', '2022-09-12 00:00:00', '', '0', 'Your Name');
INSERT INTO `databasename`.`wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '3', 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `databasename`.`wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '3', 'wp_user_level', '10');

Be sure you change ‘newsportal_db’ to the database you’re working with. Also, don’t overlook to alter the other values to those you need for the new user, as we defined in the first method.

create an admin user in WordPress

Leave Your Comment