02/06/2019 at 2:15 pm
#483817
Keymaster
@ Craig
RE: I am guessing that I need to do a snippet like I did for my other textbox to be able to import the value from a csv file but I don’t have to register it? … Do I just use these two routines?
Hmmm… you sort of lost me.
You should register all the custom text fields. And if you want to import them, you need to register them with the CSV Import functions like your snippet above does.
When adding multiple fields you do not need to add additional snippets, just update the existing ones to include all the fields.
Here’s an example which registers three fields and imports them:
// Register the metabox and fields.
add_action( 'cn_metabox', 'cn_register_custom_metabox_and_text_field' );
function cn_register_custom_metabox_and_text_field() {
$atts = array(
'title' => 'Metabox Name', // Change this to a name which applies to your project.
'id' => 'custom_one', // Change this so it is unique to you project.
'context' => 'normal',
'priority' => 'core',
'fields' => array(
array(
'name' => 'Field One', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'field_one', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'text', // This is the field type being added.
'size' => 'regular', // This can be changed to one of the following: 'small', 'regular', 'large'
),
array(
'name' => 'Field Two', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'field_two', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'text', // This is the field type being added.
'size' => 'regular', // This can be changed to one of the following: 'small', 'regular', 'large'
),
array(
'name' => 'Field Three', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'field_three', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'text', // This is the field type being added.
'size' => 'regular', // This can be changed to one of the following: 'small', 'regular', 'large'
),
),
);
cnMetaboxAPI::add( $atts );
}
// Register the custom fields CSV Import mapping options and processing callback.
add_filter( 'cncsv_map_import_fields', 'cncsv_header_name' );
add_action( 'cncsv_import_fields', 'cncsv_process_import', 10, 3 );
function cncsv_header_name( $fields ) {
// The field_id should match exactly the field id used when registering the custom field.
$fields['field_one'] = 'Field One';
$fields['field_two'] = 'Field Two';
$fields['field_three'] = 'Field Three';
return $fields;
}
function cncsv_process_import( $id, $row, $entry ) {
// The field_id should match exactly the field id used when registering the custom field.
$one = $entry->arrayPull( $row, 'field_one', '' );
$one = cnFormatting::maybeJSONdecode( stripslashes( $one ) );
$two = $entry->arrayPull( $row, 'field_two', '' );
$two = cnFormatting::maybeJSONdecode( stripslashes( $two ) );
$three = $entry->arrayPull( $row, 'field_three', '' );
$three = cnFormatting::maybeJSONdecode( stripslashes( $three ) );
cnEntry_Action::meta(
'update',
$id,
array(
array(
'key' => 'field_one',
'value' => $one
),
array(
'key' => 'field_two',
'value' => $two
),
array(
'key' => 'field_three',
'value' => $three
),
)
);
}
