Here’s a QuickTip for users of the CSV Import extension…
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 setup. Here’s little code snippet that will add the ability to import some membership meta data 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:
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 lets 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 it’s function is to allow you to add additional choices to the drop down 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 drop down. Here you can add as many new choices as you need by adding a line for each metadata item you are planning to importing.
Now that the choices have been added to the drop down you now need to add the function would actually imports 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 check to see if the metadata exists and if it does not, set it to nothing:
$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!