Importing custom user fields

Our CSV import tool allows you to import and/or update member accounts from a CSV file. It supports a set of predefined fields, and custom fields can be imported with a small amount of custom code.

Add the fields to your CSV file

First you'll need to add the custom fields to your CSV file. Each custom field should have a column heading (this name is important - we'll be using it later!) and the value should be entered for each user. For example, I might create a new column called "birthday" and my CSV file might look like this:

You can see the custom birthday field highlighted in yellow.

Importing the data

You'll need to setup a place to put the custom code. This can be in your child theme or a custom plugin file. If you need help doing this you can read  our documentation article on adding code snippets.

Here's the code we'll be adding:

/**
 * Import custom user fields.
 *
 * @param RCP_Membership $membership Newly created/updated membership object.
 * @param WP_User        $user       User associated with the membership.
 * @param array          $row        Array of data in the current CSV row.
 */
function ag_rcp_import_custom_fields( $membership, $user, $row ) {

	$birthday = $row['birthday']; // Replace 'birthday' with your column heading name.

	if ( ! empty( $birthday ) ) {
		/*
		 * Example 1: saving information as user meta.
		 *
		 * Change 'birthday' to the user meta key you'd like to save the data as.
		 */
		update_user_meta( $user->ID, 'birthday', sanitize_text_field( $birthday ) );

		/*
		 * Example 2: saving information as membership meta.
		 *
		 * Change 'birthday' to the membership meta key you'd like to save the data as.
		 */
		rcp_update_membership_meta( $membership->get_id(), 'birthday', sanitize_text_field( $birthday ) );
	}

}
add_action( 'rcp_csv_import_membership_processed', 'ag_rcp_import_custom_fields', 10, 3 );

There are a few things you'll need to edit to make this code your own:

  • Change the variable name $birthday to something that reflects your custom field. (This ultimately doesn't matter, it's just for keeping your code neatly labelled!) Update all three instances of this throughout the code.
  • In $row['birthday'] replace birthday with the name of your column heading.
  • Use EITHER example 1: for saving the value to user meta; OR example 2: for saving the value to membership meta. Delete the line you do not want to use.
  • If saving to user meta: in the update_user_meta function, replace 'birthday' with the user meta key you'd like to use when saving the data. (Use underscores instead of spaces.)
  • If saving to membership meta: in the rcp_update_membership_meta function, replace 'birthday' with the membership meta key you'd like to use when saving the data. (Use underscores instead of spaces.)

Once you've added your code, you can then proceed to import your CSV file. During the import, all custom fields will be added to the account's user meta or membership meta. You can then display this data in the admin area (with the  rcp_edit_member_after action) and/or front-facing pages (with the rcp_profile_editor_after action).

Note: the custom fields will not be used in the CSV column mapping step, but they will be quietly imported in the background when you proceed to the step after.