When working on large sites that involve integration with 3rd party data sources for user accounts, one of my go-to plugins has been Import Users from CSV with meta. I had bookmarked WP All Import and its’ User Import add-on quite some time ago as an interesting alternative, but until a recent project came up with requirements that needed substantially more flexibility / control over the import process, I was quite happy with the free, fantastic and regularly maintained wp.org repo plugins. In fact, I’d still recommend it for a lot of sites / scenarios.
The requirements
- 3rd party system provides entire list of users including multiple meta-data fields but NO role data as .csv on a regular schedule
- Import must create new users with all meta and update all meta on existing users
- Import must apply default role (employee) to all users and delete any non-matched users.
- Existing users that have multiple roles (Members plugin) must maintain their roles
The deletion of non-matched users was the feature set WP All Import apart in consideration, but it was the extensibility around addressing the last requirement for maintaining multiple roles that made the transition most enjoyable.
The Role Field in the Import Template supports custom php functions which would allow the execution to compare existing user roles. Unfortunately that field is limited to specifying a single role / string and the Members plugin stores the list of roles in wp_capabilities user meta field as a serialized / associated array. Thankfully, the Custom Fields section also supports php functions and has no value restrictions. Had the import file included the role(s), I would likely have used the serialize option. But since it didn’t the following function works well to check existing user via meta query and build an associative array of true Boolean values for their existing roles and return the serialized result.
Hi
I am trying to do the same and think this snippet and setup should work (just about to test!).
I need a default role of ‘Website Member’ for each user that joins the site, plus a specific custom role depending on what industry they are in. I’ve also used the Members plugin to set those up.
One query though:
In the WP All Import settings, where you have added a custom field of wp_capabilites (screenshot above); it also has a Field Options box which gives you a choice of Serialized or Mapping. This isn’t shown on your screenshot. Should I choose one of these options?
Many thanks
Neither of the Field Options are required to be set. The custom function provided returns a serialized array which WPAI will handle to store the role(s) against appropriate user.
The Serialize Field Option lets you build a series of key/value pairs, but doesn’t suit this case where dynamic checks against existing role(s) is required. Mapping Field Option can be useful in other scenarios where you need to pull together data from other columns in your import.