[et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

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

  1. 3rd party system provides entire list of users including multiple meta-data fields but NO role data as .csv on a regular schedule
  2. Import must create new users with all meta and update all meta on existing users
  3. Import must apply default role (employee) to all users and delete any non-matched users.
  4. 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.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Row”][et_pb_column type=”1_2″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

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.

[/et_pb_text][/et_pb_column][et_pb_column type=”1_2″][et_pb_image admin_label=”Image” src=”https://idealienstudios.com/wp-content/uploads/wpallimport.jpg” show_in_lightbox=”on” url_new_window=”off” animation=”off” sticky=”off” align=”left” force_fullwidth=”off” always_center_on_mobile=”on” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]
[/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

https://gist.github.com/Idealien/fefbf9509b6a00946ed0322c85a02fec

 

[/et_pb_text][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

While WordPress does a wonderful job at abstracting the deep details of the DB from users and most SQL from most development use-cases, it’s essential to check out initial implementations of functions to compare results b/t entries generated via WP admin UI and ones imported / updated via code.

[/et_pb_text][et_pb_image admin_label=”Image” src=”https://idealienstudios.com/wp-content/uploads/wpallimport-DB.jpg” show_in_lightbox=”on” url_new_window=”off” animation=”off” sticky=”off” align=”left” force_fullwidth=”off” always_center_on_mobile=”on” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] [/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Row”][et_pb_column type=”1_3″][/et_pb_column][et_pb_column type=”2_3″][/et_pb_column][/et_pb_row][/et_pb_section]

Similar Posts