Last active
February 14, 2024 08:46
-
-
Save andrewlimaza/50a3d1ab70c09428b6236fa35a932c0d to your computer and use it in GitHub Desktop.
Import simple array into WordPress using Import Users From CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/