Skip to content

Instantly share code, notes, and snippets.

@SamuelHadsall
Last active June 6, 2023 17:47
Show Gist options
  • Save SamuelHadsall/1e170da08ee727b8bb1fe30834f25860 to your computer and use it in GitHub Desktop.
Save SamuelHadsall/1e170da08ee727b8bb1fe30834f25860 to your computer and use it in GitHub Desktop.
Imports csv and maps to acf fields
<?php
if (!function_exists('acf_add_local_field_group')) {
return;
}
acf_add_local_field_group([
'key' => 'demographics_5b73377adf04f',
'title' => 'Demographics',
'fields' => [
[
'key' => 'group_demographics_5b7337960d672',
'label' => 'Primary Reason for Moving',
'name' => 'reason',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '30',
'class' => '',
'id' => '',
],
'layout' => 'row',
'sub_fields' => [
[
'key' => 'field_5b73381f0d673',
'label' => 'Retirement (In)',
'name' => 'retirement_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b77211a346a5',
'label' => 'Retirement (Out)',
'name' => 'retirement_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b7338600d674',
'label' => 'Health/Personal (In)',
'name' => 'health_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b7725b8346a6',
'label' => 'Health/Personal (Out)',
'name' => 'health_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b7338700d675',
'label' => 'Family (In)',
'name' => 'family_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b7725d7346a7',
'label' => 'Family (Out)',
'name' => 'family_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b73387c0d676',
'label' => 'Lifestyletest (In)',
'name' => 'Lifestyletest_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b77262cfa94e',
'label' => 'Lifestyletest (Out)',
'name' => 'Lifestyletest_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b73388e0d677',
'label' => 'Job (In)',
'name' => 'job_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772674fa94f',
'label' => 'Job (Out)',
'name' => 'job_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],[
'key' => 'field_618e7779abd6d',
'label' => 'Cost of Living (In)',
'name' => 'cost_of_living_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],[
'key' => 'field_618e79b748904',
'label' => 'Cost of Living (Out)',
'name' => 'cost_of_living_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
],
],
[
'key' => 'group_demographics_5b7726bdf3e56',
'label' => 'Age Ranges',
'name' => 'age',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '30',
'class' => '',
'id' => '',
],
'layout' => 'row',
'sub_fields' => [
[
'key' => 'field_5b772a6504d19',
'label' => '<18 to 34 (In)',
'name' => '18_to_34_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772c8808eb2',
'label' => '<18 to 34 (Out)',
'name' => '18_to_34_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772ca208eb3',
'label' => '35 to 44 (In)',
'name' => '35_to_44_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772cc508eb4',
'label' => '35 to 44 (Out)',
'name' => '35_to_44_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772ccf08eb5',
'label' => '45 to 54 (In)',
'name' => '45_to_54_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772ce708eb6',
'label' => '45 to 54 (Out)',
'name' => '45_to_54_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772d3da2fb8',
'label' => '55 to 64 (In)',
'name' => '55_to_64_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772d55a2fb9',
'label' => '55 to 64 (Out)',
'name' => '55_to_64_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772d60a2fba',
'label' => '65 or Older (In)',
'name' => '65_or_older_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772d83a2fbb',
'label' => '65 or Older (Out)',
'name' => '65_or_older_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
],
],
[
'key' => 'group_demographics_5b772daf7b3b9',
'label' => 'Income',
'name' => 'income',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '40',
'class' => '',
'id' => '',
],
'layout' => 'row',
'sub_fields' => [
[
'key' => 'field_5b772db07b3ba',
'label' => '$0 to $49,999 (In)',
'name' => '0_49_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3bb',
'label' => '$0 to $49,999 (Out)',
'name' => '0_49_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3bc',
'label' => '$50,000 to $74,999 (In)',
'name' => '50_74_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3bd',
'label' => '$50,000 to $74,999 (Out)',
'name' => '50_74_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3be',
'label' => '$75,000 to $99,999 (In)',
'name' => '75_99_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3bf',
'label' => '$75,000 to $99,999 (Out)',
'name' => '75_99_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3c0',
'label' => '$100,000 to $149,999 (In)',
'name' => '100_149_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3c1',
'label' => '$100,000 to $149,999 (Out)',
'name' => '100_149_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3c2',
'label' => '$150,000 or More (In)',
'name' => '150_more_in',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
[
'key' => 'field_5b772db07b3c3',
'label' => '$150,000 or More (Out)',
'name' => '150_more_out',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '.01',
],
],
],
],
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'location',
],
[
'param' => 'post_template',
'operator' => '!=',
'value' => 'template-location-year.php',
],
],
],
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'left',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
]);
public function csv_importer_handle_import() {
// Check if the form is submitted and the user has the necessary capabilities
if (
isset($_POST['csv_importer_nonce']) &&
wp_verify_nonce($_POST['csv_importer_nonce'], 'csv_importer') &&
current_user_can('manage_options') &&
isset($_FILES['csv_file']['tmp_name'])
) {
$csv_file = $_FILES['csv_file']['tmp_name'];
$csv = array();
$i = 0;
// Load the CSV file
if (($handle = fopen($csv_file, 'r')) !== FALSE) {
// Get the custom post type and ACF field keys
$post_type = 'location';
$acf_fields = array(
'field_5b7af4746e30f' => 'state',
'group_demographics_5b7337960d672' => array(
array(
'field_5b73381f0d673' => 'reason_retirement_in',
'field_5b77211a346a5' => 'reason_retirement_out',
'field_5b7338600d674' => 'reason_health_in',
'field_5b7725b8346a6' => 'reason_health_out',
'field_5b7338700d675' => 'reason_family_in',
'field_5b7725d7346a7' => 'reason_family_out',
'field_5b73387c0d676' => 'reason_lifestyle_in',
'field_5b77262cfa94e' => 'reason_lifestyle_out',
'field_5b73388e0d677' => 'reason_job_in',
'field_5b772674fa94f' => 'reason_job_out',
'field_618e7779abd6d' => 'reason_cost_of_living_in',
'field_618e79b748904' => 'reason_cost_of_living_out',
)
),
'group_demographics_5b7726bdf3e56' => array(
array (
'field_5b772a6504d19' => 'age_18_to_34_in',
'field_5b772c8808eb2' => 'age_18_to_34_out',
'field_5b772ca208eb3' => 'age_35_to_44_in',
'field_5b772cc508eb4' => 'age_35_to_44_out',
'field_5b772ccf08eb5' => 'age_45_to_54_in',
'field_5b772ce708eb6' => 'age_45_to_54_out',
'field_5b772d3da2fb8' => 'age_55_to_64_in',
'field_5b772d55a2fb9' => 'age_55_to_64_out',
'field_5b772d60a2fba' => 'age_65_or_older_in',
'field_5b772d83a2fbb' => 'age_65_or_older_out',
)
),
'group_demographics_5b772daf7b3b9' => array(
array(
'field_5b772db07b3ba' => 'income_0_49_in',
'field_5b772db07b3bb' => 'income_0_49_out',
'field_5b772db07b3bc' => 'income_50_74_in',
'field_5b772db07b3bd' => 'income_50_74_out',
'field_5b772db07b3be' => 'income_75_99_in',
'field_5b772db07b3bf' => 'income_75_99_out',
'field_5b772db07b3c0' => 'income_100_149_in',
'field_5b772db07b3c1' => 'income_100_149_out',
'field_5b772db07b3c2' => 'income_150_more_in',
'field_5b772db07b3c3' => 'income_150_more_out'
)
),
// Add more fields as needed
);
// Headrow
$columns = fgetcsv($handle, 4096, ',', '"');
// Loop through each row in the CSV file
while (($row = fgetcsv($handle, 4096, ',')) !== FALSE) {
// This is a great trick, to get an associative row by combining the headrow with the content-rows.
$csv[$i] = array_combine($columns, $row);
$title = $csv[$i]['state'];
$post_data = array(
'post_title' => $title,
'post_type' => $post_type,
'post_status' => 'publish',
// Set any other post data as needed
);
// Create the custom post
$post_id = wp_insert_post($post_data);
// Map the CSV data to ACF fields
foreach ($acf_fields as $acf_group_key => $acf_group_fields) {
foreach ($acf_group_fields as $acf_sub_field_key => $fields) {
foreach ($fields as $acf_field_key => $csv_column) {
if (isset($csv[$i][$csv_column])) {
update_field($acf_group_key, array( $acf_field_key => $csv[$i][$csv_column] ), $post_id);
}
}
}
}
$i++;
}
fclose($handle);
}
// Redirect back to the plugin page after import
wp_safe_redirect(admin_url('tools.php?page=uvl-migration-map'));
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment