Your cart is currently empty!
Import WordPress users from CSV text
Function to import a string of CSV data into WordPress Users and WooCommerce customer fields.
function ccom_import_users_from_csv() {
$csv = '';
// Process Data
$data = explode( "\n", $csv );
foreach( $data as $row ) {
list(
$billing_company,
$billing_first_name,
$billing_last_name,
$billing_phone,
$user_email,
$billing_address,
$shipping_address
) = str_getcsv( trim( $row ) );
list( $billing_address_1, $billing_city, $billing_state, $billing_postcode )
= explode( ', ', $billing_address );
list( $shipping_address_1, $shipping_city, $shipping_state, $shipping_postcode )
= explode( ', ', $shipping_address );
$user_name = substr( $user_email, 0, strpos( $user_email, '@' ) );
$user_password = wp_generate_password( 12, false );
// Create Or Update User
$user_id = '';
$user_id = username_exists( $user_name );
if( ! $user_id ) {
$user_id = wp_create_user( $user_name, $user_password, $user_email );
}
if( $user_id && ! is_wp_error( $user_id ) ) {
update_user_meta( $user_id, 'first_name', $billing_first_name );
update_user_meta( $user_id, 'last_name', $billing_last_name );
update_user_meta( $user_id, 'billing_first_name', $billing_first_name );
update_user_meta( $user_id, 'billing_last_name', $billing_last_name );
update_user_meta( $user_id, 'billing_phone', $billing_phone );
update_user_meta( $user_id, 'billing_company', $billing_company );
update_user_meta( $user_id, 'billing_address_1', $billing_address_1 );
update_user_meta( $user_id, 'billing_city', $billing_city );
update_user_meta( $user_id, 'billing_state', $billing_state );
update_user_meta( $user_id, 'billing_postcode', $billing_postcode );
update_user_meta( $user_id, 'shipping_address_1', $shipping_address_1 );
update_user_meta( $user_id, 'shipping_city', $shipping_city );
update_user_meta( $user_id, 'shipping_state', $shipping_state );
update_user_meta( $user_id, 'shipping_postcode', $shipping_postcode );
}
}
}
Instructions for Import WordPress users from CSV text
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- Customize the code as desired.
- Add a meaningful title.
- Select whether to run on front-end or back-end (or both).
- Click “Save and Activate”.
- Test your site to ensure it works.
- Disable if any problems, or recover.
- Repeat for live environment.
Need help modifying Import WordPress users from CSV text?
Contact me. I can help with fitting projects or refer to my partner.
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Disclaimer of warranty