Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active February 14, 2024 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/50a3d1ab70c09428b6236fa35a932c0d to your computer and use it in GitHub Desktop.
Save andrewlimaza/50a3d1ab70c09428b6236fa35a932c0d to your computer and use it in GitHub Desktop.
Import simple array into WordPress using Import Users From CSV
<?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 );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Import user meta fields that are stored as an array (select2/multi-select and checkbox)" at Paid Memberships Pro here: https://www.paidmembershipspro.com/import-user-fields-stored-as-an-array-select2-multi-checkbox/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment