Introduction
A question that’s popped up in the support forums a few times now is, how can I import into the Custom Fields metadata using the CSV Import extension. There is not a way to do this thru a point and click interface yet. This is planned to come as a feature of an extension that will allow you to create any number of custom fields and then import data into them. Ok, you say… but is there a way this can be done now. Yes, there is and it is not all that difficult to set up. Here’s a little code snippet that will add the ability to import some membership metadata when importing CSV files.
The first thing you need to do is install the Code Snippets plugin.
After Code Snippets is installed, add a new snippet with the following code:
Import
For CSV Import version 1.4.x and lower.
add_filter( 'cncsv_map_import_fields', 'cncsv_import_meta_fields_options' ); function cncsv_import_meta_fields_options( $fields ) { $fields['member_level'] = 'Membership | Level'; $fields['member_since'] = 'Membership | Since'; $fields['member_expiration'] = 'Membership | Expiration'; return $fields; } add_action( 'cncsv_import_fields', 'cncsv_import_meta_fields_import', 10, 2 ); function cncsv_import_meta_fields_import( $id, $row ) { $level = isset( $row->member_level ) ? $row->member_level : ''; $since = isset( $row->member_since ) ? $row->member_since : ''; $expiration = isset( $row->member_expiration ) ? $row->member_expiration : ''; cnEntry_Action::meta( 'update', $id, array( array( 'key' => 'Level', 'value' => $level ), array( 'key' => 'Since', 'value' => $since ), array( 'key' => 'Expiration', 'value' => $expiration ) ) ); }
For CSV Import version 2.0.x and higher.
add_filter( 'cncsv_map_import_fields', 'cncsv_import_meta_fields_options' ); function cncsv_import_meta_fields_options( $fields ) { $fields['member_level'] = 'Membership | Level'; $fields['member_since'] = 'Membership | Since'; $fields['member_expiration'] = 'Membership | Expiration'; return $fields; } add_action( 'cncsv_import_fields', 'cncsv_import_meta_fields_import', 10, 3 ); function cncsv_import_meta_fields_import( $id, $row, $entry ) { $level = $entry->arrayPull( $row, 'member_level', '' ); $since = $entry->arrayPull( $row, 'member_since', '' ); $expiration = $entry->arrayPull( $row, 'member_expiration', '' ); cnEntry_Action::meta( 'update', $id, array( array( 'key' => 'Level', 'value' => $level ), array( 'key' => 'Since', 'value' => $since ), array( 'key' => 'Expiration', 'value' => $expiration ) ) ); }
Save and activate the new code snippet.
Now you’ll be able to import “Member Level”, “Member Since” and “Membership Expiration” when importing CSV files. Here is a CSV file that you can use to test the snippet.
I know the above code may look a bit daunting and does require some basic understanding of PHP. That said let’s step thru the code to see what it does…
add_filter( 'cncsv_map_import_fields', 'cncsv_import_meta_fields_options' );

This line adds a filter to cncsv_map_import_fields. This filter is in the CSV Import extension and its function is to allow you to add additional choices to the dropdown in the field mapping step when importing a CSV file. This filter will tell CSV Import to add the options added in the cncsv_import_meta_fields_import function.
function cncsv_import_meta_fields_options( $fields ) { /** * The `key` must absolutely be unique otherwise you may end up overwriting over metadata. * The 'CSV File Header Name' will be the option name shown in the dropdown. * The CSV header name should match exactly the column header name in the CSV file. */ //$fields['key'] = 'CSV File Header Name'; $fields['member_level'] = 'Membership | Level'; $fields['member_since'] = 'Membership | Since'; $fields['member_expiration'] = 'Membership | Expiration'; return $fields; }
This is the function added by the filter which will add the membership options to the dropdown. Here you can add as many new choices as you need by adding a line for each metadata item you are planning to import.
Now that the choices have been added to the dropdown you now need to add the function that would actually import the metadata to the entry. That is accomplished with the following line of code:
add_action( 'cncsv_import_fields', 'cncsv_import_meta_fields_import', 10, 2 );
After importing an entry the cncsv_import_fields action will fire and will run any functions that were added to it. In this example it is the cncsv_import_meta_fields_import function. This is the function that will actually do the importing of your custom metadata so it is important to get this right.
The first this that must be done is to check to see if the metadata exists and if it does not set it to an empty string:
$level = isset( $row->member_level ) ? $row->member_level : ''; $since = isset( $row->member_since ) ? $row->member_since : ''; $expiration = isset( $row->member_expiration ) ? $row->member_expiration : '';
Notice member_level, member_since, and member_expiration… these are the “keys” that were added in the filter earlier in the code. It is very important that they match.
The next bit of code, that is the hard part.
cnEntry_Action::meta( 'update', // Do NOT change this. $id, // Do NOT change this. array( // Do NOT change this. array( 'key' => 'Level', 'value' => $level ), array( 'key' => 'Since', 'value' => $since ), array( 'key' => 'Expiration', 'value' => $expiration ) ) ); // Do NOT change this.
First, take note, you do not need to change the update or $id.
Notice the repeating pattern?
array( 'key' => 'Meta Key Name', 'value' => $value ),
You’ll repeat this for each item metadata item that you are importing.

The Meta Key Name will be the name shown in the Custom Fields metabox name column and $value will be the value from the CSV file and shown in the Custom Fields metabox Value column.
Hopefully, your eyes are completely glazed over and this helps you!
Export
To export the custom fields when using the CSV Export Tool they need to be registered. This is done by hooking into the cn_csv_export_fields
and cn_csv_export_fields_config
filters. The cn_csv_export_fields
filter register the name to be used for the column header in the CSV file and the cn_csv_export_fields_config
filter is the configuration that will be used by the export tool to export the data from the database. Add the following code to your existing code snippet to register the custom fields for exporting.
// Register the custom test field CSV Export attributes and processing callback. add_filter( 'cn_csv_export_fields', 'cn_register_text_field_header' ); add_filter( 'cn_csv_export_fields_config', 'cn_register_text_field_config' ); function cn_register_text_field_header( $fields ) { // The field_id should match exactly the field id used when registering the custom field. $fields['member_level'] = 'Membership | Level'; $fields['member_since'] = 'Membership | Since'; $fields['member_expiration'] = 'Membership | Expiration'; return $fields; } function cn_register_text_field_config( $fields ) { $fields[] = array( 'field' => 'member_level', // The field_id should match exactly the field id used when registering the custom field. 'type' => 5, 'fields' => '', 'table' => CN_ENTRY_TABLE_META, 'types' => NULL, ); $fields[] = array( 'field' => 'member_since', // The field_id should match exactly the field id used when registering the custom field. 'type' => 5, 'fields' => '', 'table' => CN_ENTRY_TABLE_META, 'types' => NULL, ); $fields[] = array( 'field' => 'member_expiration', // The field_id should match exactly the field id used when registering the custom field. 'type' => 5, 'fields' => '', 'table' => CN_ENTRY_TABLE_META, 'types' => NULL, ); return $fields; }