You can use something like below. Basically after form submission we get the data $entry
extract the values we want (in the example $val1, $val2, $val3
) then insert that data into the custom table with $wpdb
:
add_action('gform_after_submission', 'save_to_my_custom_table', 10, 2);
function save_to_my_custom_table($entry, $form)
{
global $wpdb;
// update for your tablename (this assumes the table lives in same database as the other wp tables)
$tablename = $wpdb->prefix . "my_custom_tablename";
// grab from values from the entry
$val1 = rgar( $entry, '1' );
$val2 = rgar($entry, '2');
$val3 = rgar($entry, '3');
// basic insert
$sql = "INSERT INTO `$tablename` (`val1`,`val1`, `val1`) values (%s, %s, %d)";
// use a prepared statement
$stmt = $wpdb->prepare($sql, $val1, $val2, $val3);
// run the query
$wpdb->query($stmt);
}
If you are wanting to connect to a database totally outside the WP app I think you might want to consider setting up an API for communicating between the two. In that case you can still approach this very similary. See below:
add_action('gform_after_submission', 'save_to_my_custom_table', 10, 2);
function save_to_my_custom_table($entry, $form)
{
global $wpdb;
// update for your tablename (this assumes the table lives in same database as the other wp tables)
$tablename = $wpdb->prefix . "my_custom_tablename";
// grab from values from the entry
$val1 = rgar( $entry, '1' );
$val2 = rgar($entry, '2');
$val3 = rgar($entry, '3');
// send POST reqest to API for other database
$endpoint="api.example.com";
$body = [
'val1' => $val1,
'val2' => $val2,
'val3' => $val3,
];
$body = wp_json_encode( $body );
$options = [
'body' => $body,
'headers' => [
'Content-Type' => 'application/json',
],
'timeout' => 60,
'redirection' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => false,
'data_format' => 'body',
];
wp_remote_post( $endpoint, $options );
}