0

I need to import some data about users from an application into the wordpress database. I'm thinking to use the wp_users and wp_users_meta tables to store these informations. While I need to create accounts for each user that is in the table I also need to store additional details and the two tables seems perfect, but I'm not sure about this because the amount of entries will grow when new users will register to the site. What is the best approach for this situation, Do I need to create a custom table or I can rely without problems on the default tables of wordpress I've mentioned before?

2
  • 2
    how many users are we talking? The number needed to cause problems will be very high
    – Tom J Nowell
    Sep 22, 2022 at 18:51
  • @TomJNowell the actual db of the app will count about 2000 users that will be imported into the wp db
    – devdev
    Sep 22, 2022 at 19:01

1 Answer 1

0

I need to import some data about users from an application into the wordpress database. I'm thinking to use the wp_users and wp_users_meta tables to store these informations.

This is good, but directly interacting with the database will not flush caches, skips PHP hooks and actions, so you should expect either stale data, or compatibility issues with plugins.

Instead either use the REST API to do it via a HTTP request, or a WP CLI command to do it via CLI. This eliminates most of the work, avoids you writing SQL, handles all caches and plugins correctly, etc.

While I need to create accounts for each user that is in the table I also need to store additional details and the two tables seems perfect, but I'm not sure about this because the amount of entries will grow when new users will register to the site.

2000 users is miniscule, WP.com has hundreds of millions of users on a single multisite. The biggest problem there is calculating the pagination on the WP Admin users screen and the actual storage of all that data. At your rate of growth you should start encounter issues in the 22nd century assuming growth remains steady.

The biggest concern is user meta querys, if you're going to be making lots of queries to find users who have a user meta with X or don't have Y then the cost of extra users will no longer be linear.

What is the best approach for this situation, Do I need to create a custom table or I can rely without problems on the default tables of wordpress I've mentioned before?

Custom tables is the most difficult, most complex, most expensive, painful, and least compatible option. Expect lots of issues if you try to use custom tables for your users.

Just use standard WP tables, and use standard WP APIs to add the data.

E.g. https://developer.wordpress.org/cli/commands/user/create/

wp user create bob [email protected] --role=author

or

wp user meta add 123 bio "Mary is an WordPress developer."

Outputting a list of WP CLI commands to a script file then running that file on the host should be trivial. Afterall that's how most SQL import/exports happen ( aka .sql files ).

5
  • ok, basically the user table for the account information and the user meta table for the additional information I need are perfect. since the users will not have the username and password, after importing them I can also generate the credentials automatically?
    – devdev
    Sep 24, 2022 at 6:37
  • yes, if you use WP CLI you can pass them in as parameters if you want to choose them. have a look at the options available, they include things like skipping email verification, password resets, etc. Also you don't need to know the username to login, the login UI will quite happily accept the email address out of the box
    – Tom J Nowell
    Sep 24, 2022 at 8:55
  • the most important thing I'm considering is that I will need to assign a default password that the user will need to change after the first login and every 90 days. I don't know what hooks can be used but I need to check. I saw that there are plugins that will give this ability, but I'm creatinga custom flow so I will write all by myself
    – devdev
    Sep 24, 2022 at 10:43
  • for random passwords, WP already behaves that way. Your problem will be the 90 day requirement, but that is a separate question ( and also poor security practice as it encourages simple passwords and increases support burdens )
    – Tom J Nowell
    Sep 25, 2022 at 8:10
  • I will need to force the user to use a secure password, but the password change after a default perido is a requirment of the client so I can only implement it. I will ask a separate question for this.
    – devdev
    Sep 25, 2022 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.