skip to Main Content

I am unable to log in to my WordPress website. The website is working fine, and user credentials are correct.

Later I found that somehow the users table got corrupted and now it got deleted.

Can you explain how I can create users tables with an admin user login credentials so that I can log in back to my WordPress?

I have access to phpMyAdmin.

3

Answers


  1. You can make a small PHP file, let’s call it make_user.php, containing the following code:

    <?php
    define('ADMIN_USER', 'my_admin_user');
    define('ADMIN_MAIL', '[email protected]');
    require_once('./wp-load.php');
    
    if (! get_user_by('email', ADMIN_MAIL)) {
      $passwd = wp_generate_password(20);
      $user_id = wp_create_user(ADMIN_USER, $passwd, ADMIN_MAIL);
      $user = get_user_by('id', $user_id);
      $user->set_role('administrator');
      echo "User created.".PHP_EOL;
      //echo "Password is: $passwd";
    } else {
      echo "User already exists.";
    }
    

    Change your ADMIN_USER (account name) and ADMIN_MAIL to whatever you want.

    Upload this to the root folder of your WP installation and call it by http://your.host/make_user.php .
    You can either decide to uncomment the line echoing the password and use this, or just reset it via the regular Forgot password function (I’d do the latter).

    Delete the file after you’re done.

    Login or Signup to reply.
  2. Assuming your table name is wp_users adjust for your prefix.

    From: wp-adminincludesschema.php Line 191

    CREATE TABLE `wp_users` (
      ID bigint(20) unsigned NOT NULL auto_increment,
      user_login varchar(60) NOT NULL default '',
      user_pass varchar(255) NOT NULL default '',
      user_nicename varchar(50) NOT NULL default '',
      user_email varchar(100) NOT NULL default '',
      user_url varchar(100) NOT NULL default '',
      user_registered datetime NOT NULL default '0000-00-00 00:00:00',
      user_activation_key varchar(255) NOT NULL default '',
      user_status int(11) NOT NULL default '0',
      display_name varchar(250) NOT NULL default '',
      PRIMARY KEY  (ID),
      KEY user_login_key (user_login),
      KEY user_nicename (user_nicename),
      KEY user_email (user_email)
    )
    

    You can also add the Primary Keys and AUTO_INCREMENT via the phpMyAdmin Interface like so:

    phpMyAdmin Screen Shot

    Then follow these instructions:

    How to Create a new Admin User for A WordPress Site via MySQL (PHPMyAdmin)?

    Login or Signup to reply.
  3. Step 1: Create user_reg.php file in wp root folder and file containing the following code

    /*
    * Code start
    */ 
    
    //Create wp_users table
    
    include 'wp-load.php';
    
    global $table_prefix, $wpdb;
    
    $tblname = 'users';
    $wp_track_table = $table_prefix . "$tblname ";
    if($wpdb->get_var( "show tables like '$wp_track_table'" ) != $wp_track_table) {
    
        $sql = "CREATE TABLE IF NOT EXISTS {$wp_track_table} (
          `ID` bigint(20) UNSIGNED NOT NULL,
          `user_login` varchar(60) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_pass` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_nicename` varchar(50) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_email` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_url` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
          `user_activation_key` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
          `user_status` int(11) NOT NULL DEFAULT '0',
          `display_name` varchar(250) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT ''
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;";
    
        require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
        dbDelta($sql);
    }
    
    
    //Create user 
    
    //start: Fill you details here 
    $user_login = 'lores'; 
    $user_email = '[email protected]';
    $user_pass = 'lores@123';
    $display_name = 'mark lores';
    $first_name = 'mark';
    $last_name = 'lores';
    //end: Fill you details here 
    
    
    $flag_1 = 0;
    $flag_2 = 0;
    
    $check_username_exists = username_exists( $user_login );
    if ($check_username_exists) {
        $flag_1 = 1;
    }
    
    $check_email_exists = email_exists($user_email);
    if ($check_email_exists) {
        $flag_2 = 1;
    }
    
    
    if ($flag_1 == 0 && $flag_2 == 0) { 
    
         $userdata = array(
                    'user_login'  =>  $user_login,
                    'user_pass'   =>  $user_pass,
                    'user_email'  =>  $user_email,
                    'display_name'=>  $display_name,
                    'first_name'  =>  $first_name,
                    'last_name'   =>  $last_name
                );
    
        $user_id = wp_insert_user($userdata);
    
        wp_update_user( array ('ID' => $user_id, 'role' => 'administrator') );
    
        if(is_wp_error($user_id)){
    
            echo $user->get_error_message();
    
        }else{
    
            echo "User created successfully";
        }
    
    
    }else{
    
        echo "User already exist";
    }
    
    
    /*
    * Code end
    */ 
    

    Step 2: Run the file on browser like
    http://example.com/user_reg.php

    Step 3: If user-created successfully then try to login with given username and password

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search