Skip Navigation

[Gelöst] Cred form updates not posted

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.43 hours from now. Thank you for your understanding.

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by John vor 6 Jahre, 6 Monate.

Assigned support staff: Beda.

Author
Artikel
#307818

I am trying to post the latitude and longitude for record being created/edited.
The code below is returning the proper information.
However, the updates are not getting posted.
Also, after this update happens, is there a way to get Cred to refresh the view and show the updates?

Thank you.
//* Function to retrieve & store latitude and longitude of an address field using Google Maps api
function geocode_address($post_id)
{
$latitude_test = get_post_meta($post_id, 'wpcf-latitude', true);
if (empty($latitude_test)){
//echo 'Empty Latitude value is ';
//echo $latitude_test;

$custom_fields_address = get_post_meta($post_id, 'wpcf-property-address1', true);
$custom_fields_city = get_post_meta($post_id, 'wpcf-city', true);
$custom_fields_state = get_post_meta($post_id, 'wpcf-state', true);
$custom_fields_country = 'United States';
$custom_fields = ($custom_fields_address.' '.$custom_fields_city.' '.$custom_fields_state.' '.$custom_fields_country);

// echo"Custom fields are $custom_fields ";
$passed_url= urlencode($custom_fields);
// echo 'passed url is ';
// echo $passed_url;

if(!empty($custom_fields))
{
$resp = wp_remote_get( "hidden link".urlencode($custom_fields)."&sensor=false" );
echo 'response code is ';
echo $resp['response']['code'];
//die();
if ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=="OK"){
echo $data->status;

$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
echo $latitude;
echo $longitude;
/*response code is 200OK34.9738935-86.2275457die(); */
update_post_meta($post_id, "wpcf-latitude", $latitude);
update_post_meta($post_id, "wpcf-longitude", $longitude);
}
}
}
}else{
//echo 'Not Empty Show Latitude value is ';
//echo $latitude_test;
//die();

}//End of else test for value
}
add_action('save_post', 'geocode_address');

#307844

Thank you for contacting us here in the Support Forum and for providing the Debug Informations

Do you submit a CRED form, and your custom function should retrieve the lat/long so to update your Custom Fields with this values?

You should use a CRED API Hook, namely creed_save_data
https://toolset.com/documentation/user-guides/cred-api/#csd

If you already do this, could you share with me your entire creed_save_data code in pastebin.com, and attach a link to your exported CRED form in your next replay?
(you may use Google Drive or DropBox to share it with me)

Please make sure all custom fields/settings are included in the CRED Export when exporting:
CRED > Settings/Import > General Settings

If you are storing the new custom field values correctly with a CRED hook, the View will return those values if the proper Fields are rendered within the View.

Please don't hesitate to inform me in case the issue persists and in case it does, could you provide me the additional Infos?

Thank you

#308154

Thanks for the Details

I rebuilt this CRED form, and it's according Custom Post Types and Fields
(I allowed me to export them directly form your site, I hope this is OK)

Running your code, I see 2 things that you should first fix, before I can proceed:

1. The Characters must be fixed. All below line:

$resp = wp_remote_get( "<em><u>hidden link</u></em>".urlencode($custom_fields)."&sensor=false" );

is not applying because of wrong syntax in the above line.
(check brackets and characters)

If you use a code editor like "Brackets" (or any of your preference), you'll see what I mean.

2. Enable wp-debug on your site, when submitting the form.

You most probably will get the same error as I did:

Parse error: syntax error, unexpected '=' in /../../

(being the unexpected "=" this one: $resp =)

After your code is correctly formatted, and not throwing errors, we can try to troubleshoot this if it still does not work.

Currently I can't proceed, because this is custom code that at some point goes above the scope of support in this forum.

I can help troubleshoot Code related to our API and will of course help you troubleshoot also this one, but I can't fix the code you plan to use in this case.

I apologize the inconvenience this may cause.

Can you double-check the code is correctly formatted and working?

Please don't hesitate to inform me in case the issue persists

Thank you

#308547

I changed the code that you requested. Replaced double quotes with single quotes.
Code posts the word 'Array' in the record, rather than the latitude and longitude values.
However, the value has been correctly returned by Google.
You can see it if you force the script to die. The concatenated values read

response code is 200OK34.5265429-86.69505
Is the value in add_post_meta($post_id, "wpcf-latitude", $latitude); not being referenced properly?

Thank you.

#308624

Thanks for the Details

This should work, as you provide date code above:
add_post_meta($post_id, "wpcf-latitude", $latitude)
Make sure wpcf-custom_field_slug is correct, as well the $meta_value
Try to use single quotes ('') instead of double quotes ("")

You can also try update_post_meta:
https://codex.wordpress.org/Function_Reference/update_post_meta

The CRED Hook would need to look similar to this:

function my_save_data_action($post_id, $form_data){

            // Change your CRED Form "ID" accordingly below
            if ($form_data['id']==ID){
               
                //Declare the content with which you want to update post_meta
                //Your Custom code that receives Google's data goes here;
                //define your new $meta_value to use later on
               
                //Adjust update_post_meta accordingly with $meta_key and $meta_value
                //$prev_value is optional, as only necessary if previous value is available 
                update_post_meta($post_id, $meta_key, $meta_value, $prev_value);
               
            }
        }
    add_action('cred_save_data', 'my_save_data_action',10,2);

Please don't hesitate to inform me in case the issue persists
and let me know if the above solution works for you, I look forward to your reply!

Thank you

#308632

The issue turned out to be a flaw in my logic. A 200 response code does not mean things are good. It simply means we got a response. Digging deeper showed that when the address could not be found by Google a code was returned showing that the address could not be found.

This code is a bit clumsy, but it works.
//* Function to retrieve & store latitude and longitude of an address field using Google Maps api
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==629 || $form_data['id']==1916) {
$latitude_test = get_post_meta($post_id, 'wpcf-latitude', true);
$longitude_test = get_post_meta($post_id, 'wpcf-longitude', true);
if (empty($latitude_test)){
//echo 'Empty Latitude value is ';
//echo $latitude_test;

$custom_fields_address = get_post_meta($post_id, 'wpcf-property-address1', true);
$custom_fields_city = get_post_meta($post_id, 'wpcf-city', true);
$custom_fields_state = get_post_meta($post_id, 'wpcf-state', true);
$custom_fields_country = 'United States';
$custom_fields = ($custom_fields_address.' '.$custom_fields_city.' '.$custom_fields_state.' '.$custom_fields_country);

// echo"Custom fields are $custom_fields ";
$passed_url= urlencode($custom_fields);
// echo 'passed url is ';
// echo $passed_url;

if(!empty($custom_fields))
{
$resp = wp_remote_get('hidden link'.urlencode($custom_fields).'&sensor=false');
echo 'response code is ';
echo $resp['response']['code'];
//die();
if (!200 == $resp['response']['code'] ) {
echo "bad response";
die();
}

if ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=='OK'){
echo "status is ";
echo $data->status;

$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
echo " Latitude is ";
echo $latitude;
echo $longitude;
//die();
/*response code is 200OK34.5265429-86.69505 die(); */
update_post_meta($post_id, 'wpcf-latitude',$latitude,$latitude_test);
update_post_meta($post_id, 'wpcf-longitude',$longitude,$longitude_test);

}else{
echo " data status is not OK ";
echo $data->status;
// die();
} //end of data status is OK
} //end of test for 200 response code
}// not empty custom fields

}else{
// Latitude already has a value
echo 'Not Empty Show Latitude value is ';
echo $latitude_test;
// die();
} //End of else test for value

}else{
echo "neither form was detected.";
// die();

}//end of form test die if not the form we seek
} //end of function

add_action('cred_save_data', 'my_save_data_action',10,2);