• Resolved Curtis

    (@curtis782)


    CSV column name: field_map_location

    I need to program a variation of the following in the cell to import correctly:

    Array(
    [address] => 123 Main Street, City, ST 12345, USA
    [lat] => 40.123456789
    [lng] => -73.123456789
    )

    Thanks for any help you can provide.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hey Curtis, I have just been banging my head on this for day and found the answer to be a walk in the park with this plugin. Here is my fresh-off-the-press review of the plugin where I explain the basic.

    So in your case this is what I would do. I assume that your field, field_map_location is a meta field, format the data using the following string structure,

    
    address|123 Main Street, City, ST 12345, USA||lat|40.123456789||lng|-73.123456789
    

    Import the CSV as usual, and hook the filter really_simple_csv_importer_save_meta and convert your cell string to the array format needed such as,

    add_filter('really_simple_csv_importer_save_meta', 'address_to_array',10,3);
    function address_to_array($meta, $post, $is_update){
      $address = $meta['field_map_location'];
      $address = explode('||',$address);
      $meta_arr = array();
      foreach($address as $sub){
        $sub = explode('|',$sub);
        $meta_arr[$sub[0]] = $sub[1];
      }
      $meta['field_map_location'] = $meta_arr;
      return $meta;
    }

    that should do it I reckon.

    Thread Starter Curtis

    (@curtis782)

    Hi Aurovrata, thanks for your detailed reply. I unfortunately only have a very basic programming background so I was unable to follow after the data format advice. Can you please explain where I can reference the filter hook and the code snippet you provided? Thanks again!

    hi all!

    @curtis782
    A way to place the code snippet to hook the filter would be to place the code snippet in its own separate small helper plugin. Such as here:

    @aurovrata , was that how you added the code snippet in your case? How did you do it? ( I think what curtis782 is asking is for further guidance on how to implement the code snippet.)

    placing the code snippet in its own separate small helper plugin:

    <?php
    /*
    Plugin Name: Divide Meta Fields with Double-pipe (Really Simple CSV Importer add-on)
    */
    
    // format from rscsv-divide-meta-fields.php https://gist.github.com/hissy/d2041481a72510b7f394
    // from https://www.ads-software.com/support/topic/how-can-i-setup-an-array-for-map-data-in-a-csv-field/#post-8343927
    
    add_filter('really_simple_csv_importer_save_meta', 'address_to_array',10,3);
    function address_to_array($meta, $post, $is_update){
      $address = $meta['field_map_location'];
      $address = explode('||',$address);
      $meta_arr = array();
      foreach($address as $sub){
        $sub = explode('|',$sub);
        $meta_arr[$sub[0]] = $sub[1];
      }
      $meta['field_map_location'] = $meta_arr;
      return $meta;
    }
    
    ?>
    
    Thread Starter Curtis

    (@curtis782)

    Thanks @mjjojo for your feedback. I have deactivated unnecessary plugins and have the “Really Simple CSV Importer” plugin installed/active, along with the “Divide Meta Fields with Double-pipe (Really Simple CSV Importer add-on)” plugin.

    When I import data in the format (on one line):
    12 Main Street, City, ST 12345, USA||lat|40.123456789||lng|-73.123456789
    that gets imported without changes. I have tried importing to cf47rs_map_location and cf47rs_property_map_location without luck.

    I am not sure I am approaching this correctly (for a CSV import), but this is from the theme developer if it helps:

    To create a valid location entry, you should create these 2 meta fields:
    1. “cf47rs_map_location” field with serialized array of the following values (example)

    Array(
        [address] => 12 Main Street, City, ST 12345, USA
        [lat] => 40.123456789
        [lng] => -73.123456789
    )

    2. Map the “cf47rs_map_location” field to the global meta field identifier “field_cf47rs_property_map_location”

    In XML format it looks like this:

    <wp:postmeta>
    <wp:meta_key><![CDATA[cf47rs_map_location]]></wp:meta_key>
    <wp:meta_value><![CDATA[a:3:{s:7:"address";s:36:"12 Main Street, City, ST 12345, USA";s:3:"lat";s:17:"40.123456789";s:3:"lng";s:16:"-73.123456789";}]]></wp:meta_value>
    </wp:postmeta>
    
    <wp:postmeta>
    <wp:meta_key><![CDATA[_cf47rs_map_location]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_cf47rs_property_map_location]]></wp:meta_value>
    </wp:postmeta>
    

    Thanks for any additional help you can provide ??

    PS: Serialization is not correct in the code, but provided as an example.

    • This reply was modified 8 years, 4 months ago by Curtis.
    • This reply was modified 8 years, 4 months ago by Curtis.
    • This reply was modified 8 years, 4 months ago by Curtis.
    Thread Starter Curtis

    (@curtis782)

    Update: I was able to get this to work. In the custom plugin code, I actually had to reference “cf47rs_map_location” instead. For the support mapping requirement mentioned by the theme developer, I created another column, “_cf47rs_map_location” and filled that with “field_cf47rs_property_map_location”. Seems to work great now. Thanks everyone for your patience and help with this!

    Dear Curtis apologies, I made the assumption you would know how to do this ??

    I added the code in my functions.php in my theme folder, which I remove once I have imported my WP content. However if you are looking to make a this a permanent feature, ie you have to regularly import such data, then @mjjojo solution is more appropriate as it ensures the functionality will work even if you change your theme later on.

    @aurovrata sounds like your code is working together with Curtis efforts, to get it solved.

    @curtis782 that’s great that it’s working! Also, thanks for having written back with more details of what the full solution turned out to be.

    Thanks for having marked this thread as resolved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How can I setup an array for map data in a csv field?’ is closed to new replies.