Gravity Forms添加单选按钮后无法通过gform_after_submission提交数据至Pardot
Hey there, let's tackle this problem head-on! The root issue here is that Gravity Forms returns radio button values as an array (even for single selections) in the gform_after_submission hook, but Pardot's API expects a single string value. That mismatch is causing your submission to fail. Here's how to fix it:
Step 1: Confirm the Radio Field Value Format
First, let's verify that the value is indeed an array. Add a quick debug line to your hook function to check:
add_action('gform_after_submission', 'send_to_pardot', 10, 2); function send_to_pardot($entry, $form) { // Replace with your actual radio field ID error_log('Raw radio value: ' . print_r($entry['12'], true)); // Rest of your code... }
Check your WordPress error log, and you'll likely see something like Array ( [0] => "Your Selected Option" ) instead of a plain string.
Step 2: Extract the Single Value from the Array
Since radio buttons only allow one selection, we can safely grab the first element of the array. Use this logic to handle both array and string cases (in case GF ever changes this behavior):
// Replace '12' with your radio field's ID $radio_field_id = '12'; $radio_value = is_array($entry[$radio_field_id]) ? reset($entry[$radio_field_id]) : $entry[$radio_field_id];
The reset() function pulls the first element of the array, which is the user's selected option.
Step 3: Update Your Pardot Submission Code
Now swap out the raw array value with your processed single value when building the data for Pardot. Here's a complete example:
add_action('gform_after_submission', 'send_to_pardot', 10, 2); function send_to_pardot($entry, $form) { // 1. Process the radio button value $radio_field_id = '12'; // Your GF radio field ID $radio_value = is_array($entry[$radio_field_id]) ? reset($entry[$radio_field_id]) : $entry[$radio_field_id]; // 2. Build Pardot-compatible data $pardot_payload = array( 'email' => $entry['1'], // Example: your GF email field ID 'pardot_radio_field_key' => $radio_value, // Replace with Pardot's field key // Add other required fields here... ); // 3. Submit to Pardot API $response = wp_remote_post('https://your-pardot-api-endpoint.com', array( 'body' => $pardot_payload, 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded', // Include any auth headers Pardot requires ), )); // Optional: Log results for debugging if (is_wp_error($response)) { error_log('Pardot submission failed: ' . $response->get_error_message()); } else { error_log('Pardot submission succeeded!'); } }
Key Notes to Double-Check
- Field Matching: Make sure the value you're sending matches exactly what Pardot expects. For example, if Pardot's radio option value is "blue" but your GF field displays "Blue", you might need to map the GF label to the Pardot value (use
$entry[$radio_field_id . '.3']if you need the value instead of the label—replace 3 with your option's value ID). - Debugging: Keep using
error_log()to verify the processed$radio_valueis a plain string before sending it to Pardot. - API Requirements: Confirm Pardot's API documentation for the specific field's data type—some fields might require numeric values instead of strings.
内容的提问来源于stack exchange,提问作者Syklops

