您可以使用下面的内容。基本上,在提交表单后,我们可以获得数据$entry
提取我们想要的值(在示例中$val1, $val2, $val3
) 然后使用将该数据插入到自定义表中$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);
}
如果你想连接到一个完全在WP应用程序之外的数据库,我想你可能需要考虑设置一个API来进行两者之间的通信。在这种情况下,您仍然可以非常类似地处理此问题。见下文:
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 );
}