• Resolved Jarri_81

    (@jarri_81)


    Hi Aurovrata,

    I am trying to filter and address which I am retrieving through a cf7 form n order to convert it to a pair of values (LAT. & LONG.).

    I do have the function that actually converts address in the couple of values but I am struggling in creatign a couple of filters to actually apply the function.

    Could you help me with it?

    The above mentioned function is as follows:

    function get_lat_long($address){
     
            $address = str_replace(" ", "+", $address);
         
            $json = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
            $json = json_decode($json);
         
            $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
            $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
            return $lat.','.$long;
    }

    Kind regards,

    Javier

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    This is a google maps PHP api question. You should really ask this question in the appropriate stackoverflow channel.

    However, I don’t fully understand if you need help with the Google Maps API or with saving your lat&lng as a post meta to your mapped form post?

    Plugin Author Aurovrata Venet

    (@aurovrata)

    If you’re looking to add the lat & lng as meta fields to your mapped post when a submission is made, then you can do with this action cf7_2_post_form_mapped_to_{$post_type} which is fired when the post has been mapped,

    add_action('cf7_2_post_form_mapped_to_my-cpt','modify_form_posts',10,2);
    function modify_form_posts($post_id, $cf7_form_data){
      //cf7_form_data is the submitted data from your form
      //post_id is the id of the post to which it has been saved.
      //... do something here
    }
    
    Thread Starter Jarri_81

    (@jarri_81)

    Hi Aurovrata,

    Maybe I didn’t explain the situation properly.

    I have no issue with google maps api or the function to retrieve latitude and longitude from an address.

    My problem is probably in understanding action & filter hooks and the difference between them.

    Before reading your answer I was trying to use a filter (as propossed in your plugin -I am still using version 1.3.2 since I could not solve the issue with later versions not allowig me to add metadata). My attempt to get latitude was to use your plugin option “Hook with a filter” in order to map a custom meta field called LAT with the following filter added to functions.php:

    add_filter('cf7_2_post_filter-my_post_type-LAT','get_lat',10,2);
    function get_lat($LAT, $form_data){
          
            $json = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=$form_data['my_field_containing_location]'&sensor=false&region=$region");
            $json = json_decode($json);
         
            $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
            return $LAT;
    }

    Similarly, I would map LONG. However these are my first steps into this kind of code so I am assuming (maybe not correctly) that in the moments of executing the filter $form_data[‘my_field_containing_location’] will contain the posted location.

    Then, you have proposed using add_action instead of add_filter which adds more uncertainty to my understanding of what I am doing ?? .

    Summarizing:

    Is $form_data[‘my_field_containing_location’] a valid statement to be included in the function?

    Should I use actions instead of filters? How? The plugin only allows my to Hook with filter

    Thanks a lot,

    Javier

    Plugin Author Aurovrata Venet

    (@aurovrata)

    HI Javier

    If you are confused you should to read up about the difference between Actions and Filters as WordPress hooks. An action allows you to insert your own custom functionality at particular point of an execution flow. A filter on the other hand allows you to filter a particular value and change it.

    Some hooks are actions while others are filters, you need to make sure you know which one is which from the documentation.

    In your example, you are correctly using a filter to change the value of a post meta field (name = LAT) which is being saved to the post type my_post_type. Since you selected the filter option for populating your post meta-field instead of a form field, the plugin expects you to supply the value. Your function should work.

    However, in your original question you were asking me how to save bot the lat & lng. One solution is to provide 2 post meta-field that you filter (like your example above) each in turn but is not efficient as you end up making 2 calls to the google api service.

    Instead you can use an action hook such as the one I suggested. When the plugin has finished saving your form, it fires the action hook and passes the newly created/mapped post id along with the submitted form data. You can therefore run your function and save both lat & lng in one function using the update_post_meta function.

    Thread Starter Jarri_81

    (@jarri_81)

    Hi Aurovrata,

    That was very helpful!! Thanks a lot!!

    Javier

    Plugin Author Aurovrata Venet

    (@aurovrata)

    ur wc.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filtering an address’ is closed to new replies.