This post is the second part in a series I’m working through that talks about WordPress Metadata Association, and the first part of this involves working through the process of programmatically creating WordPress users (but it’s been five years!).
I’ve talked about doing this before, but things change over time as we gain more experience, right? And in working through a series like this, I think it helps to have a set of requirements we can follow, at the very least, to simulate what it might be like to have this happen in a real project.
So in this particular post, we’re going to take a look at the following:
- Receiving user information in a particular data format,
- Identifying what information is required to create a user,
- Creating the user,
- Making sure our code is up to par with coding standards and readability.
It sounds like a lot, though I’ve no intentions of making this a long post. That said, if this is something you’re looking to learn how to think through or how to tackle, then here we go.
Creating WordPress Users
Assume that someone – perhaps a client or maybe even just yourself – as information that will be used to create a user. It needs at least the following information:
- a username,
- a password,
- an email address.
When it comes to creating a username, I tend to favor using the email address as the username as well because it’s something that’s guaranteed to be unique to almost anyone who uses the account.
So for this tutorial, that’s exactly what I’m going to use. As far as the password is concerned, I’ll talk about password generation momentarily.
Finally, you can assume the data you have comes in any form: Perhaps it’s in JSON, perhaps it’s in a CSV, perhaps it’s in an XML. Whatever the case, it’s up to you to parse this information into PHP so you can easily work with the information.
To keep things simple for this post, I’m going to assume we have a single record and we’re going to have the information available in an array. This doesn’t mean it starts in an array, but it means we eventually place it into an array.
1. The User’s Information
Assume we’ve got a person named “Meghan” we’re going to be adding to the system. We have an email address, and that’s it.
But that’s okay. It’s the barebones of what we need. So let’s take this information:
And turn it into something we can use to create an account.
2. Creating the Account
The first thing we need to do is make sure a user doesn’t already exist for this email address. If it does, then we’ll simply return. You may want to display a settings message or some other type of information to let the end-user know you aren’t creating this account because it already exists.
But that’s beyond the scope of this post.
Instead, we’re going to return:
Then again, let’s assume the user doesn’t exist. If she doesn’t – which she shouldn’t (otherwise, there’s no reason for this post 😏) – then we’re going to create an account for her.
To do this, we need her email address and a password. Luckily, generating a password is simple:
So now we can take the email address and the password and create the user account.
I’m opting to use author, but there’s a set of others from which you can choose (or use whatever is in your data structure).
3. Isn’t There More to It?
And with that, your user should be created. Part of the this largely depends on the hook you’ve used, how you’ve implemented this into your process, and the way in which you’re allowing an administrator to create accounts.
This is a more advanced topic that I’d eventually like to cover in a future post. To keep this series as direct and as lean as possible, I’m trying to stay as focused on the core steps required.
Everything else can be implemented or dressed up around whatever else is needed as per the requirements of the project.
That’s Just Creating an Account
This is just the processing of programmatically creating WordPress users, though. In fact, this is just creating one user.
To create multiple users, assume that you have multiple arrays with information through which you can iterate and then create the users. The implementation is the same though it will vary from each account.
With that said, tomorrow I’m going to take about programmatically creating post content programmatically. And after that, we’ll talk about relating a given user to the given post content and what we can do with that (as well as why this would even matter).
- WordPress Metadata Association: How To Do It
- Programmatically Creating WordPress Users
- WordPress Post Types: An Abstraction For Entities
- WordPress Metadata Association: Relating Entities