Migs
Migs

Reputation: 11

gform_after_submission syntax to send data to second table

I am using a 3rd party plugin which creates a custom post, and stores some of the post fields in a custom table. I am also using Gravity Forms to submit the custom post type, but cannot get the necessary field data into the plugin-created table.

All the relevant standard fields in the custom post type are populated on submission of the Gravity Form just fine, when stored in the standard tables. Some custom fields are also populated correctly which is being done by using GF Post Fields > Custom Field in the form, from which the correct custom field is selected for each form field.

The problem is the 2 fields which are sent by the plugin to a different table. I simply cannot get these to populate. Here is the code I am using, where "wc_locations_relationships" is the table name, and "location_id" and "address_field_1" are the field names.

add_action('gform_after_submission_2', 'wc_add_entry_to_db', 10, 2);
function wc_add_entry_to_db($entry, $form) {

    $source = $entry['source_url'];

      $wccountry =  $entry['30'];
      $wcaddressline1 = $entry['24'];


    global $wpdb;

    // add form data to custom database table
    $wpdb->insert(
        'wc_locations_relationships',
        array(
          'source_url' => $source,
          'location_id' => $wccountry,
          'address_line_1' => $wcaddressline1
        )
    );

}

When I submit the form, it is posted correctly, and appears in the plugin post list in admin. With regards to the code above, there are no errors, and nothing appears in those fields in the post created by the plugin. The standard WP title field and others not sent to the second table all populate correctly.

All help greatly appreciated, as this is definitely not my strong point where WP is concerned.

Upvotes: 1

Views: 611

Answers (1)

dvs.spy
dvs.spy

Reputation: 65

Try this. I hope it will work. I have been working with this code and it has been working for me in all the cases.

$wpdb->insert(
    'wc_locations_relationships',
    array(
      'source_url' => $source,
      'location_id' => $wccountry,
      'address_line_1' => $wcaddressline1
    ),
    array("%s", "%s", "%s")
);

Upvotes: 0

Related Questions