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.
- 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.