Our Import Users from CSV Integration Add On allows you to import user or user meta fields for current members. This recipe demonstrates how to import data that is stored as an “array” (for multi-select or checkbox values). It’s a useful guide for sites that have added checkbox or select2 custom user field types.

How to import user meta fields that are stored as an array

The Code Recipe

<?php
/**
* In your CSV file, you may set the array value to {value1,value2,value3}
* This will import array values into WordPress.
* To add this code to your site follow this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
* Integrates with Import Users From CSV - https://wordpress.org/plugins/import-users-from-csv/
*/
function my_import_user_meta_csv_array( $meta, $userdata ) {
// Loop through meta to get arrays.
foreach( $meta as $key => $value ) {
$reg = '/( { ( (?: [^{}]* | (?1) )* ) } )/x';
preg_match_all( $reg, $value, $matches );
if ( ! empty( $matches[0] ) ) {
$string = substr( $matches[0][0], 1, -1 ); // remove curly braces.
$array = explode( ',', $string ); // convert to PHP array.
$meta[$key] = $array; // assign the key to an array.
}
}
return $meta;
}
add_filter( 'pmproiucsv_import_usermeta', 'my_import_user_meta_csv_array', 10, 2 );

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

To prepare your CSV for import, you’ll need to add the appropriate new meta field as a column of data. For the field values, follow the format {value1,value2,value3}.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics


Was this article helpful?
YesNo