使用 WordPress Gravity Forms 将数据发送给第三方

Send data to third party with WordPress Gravity Forms

我在 functions.php 中使用以下 code 通过 cURL 提交给第三方。 问题是这段代码对我所有的表单实例都是通用的(我的 WP 网站上有无数的重力表单,我只需要一个就可以参加这个第 3 方)

add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {

    $post_url = 'http://thirdparty.com';
    $body = array(
        'first_name' => rgar( $entry, '1.3' ), 
        'last_name' => rgar( $entry, '1.6' ), 
        'message' => rgar( $entry, '3' ),
        );

    $request = new WP_Http();
    $response = $request->post( $post_url, array( 'body' => $body ) );

}

如何select使用这个第三方调用的表单?

您 post 编辑的 link 几乎包含您需要了解的有关重力形式的所有文档。您需要做的就是检查 $form 参数以获取当前表单使用的任何标识符:

add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
    if($form['title'] !== 'your-form-title'){ //Check the title
        return; //If the title doesn't match, don't POST
    }
    $post_url = 'http://thirdparty.com';
    $body = array(
        'first_name' => rgar( $entry, '1.3' ), 
        'last_name' => rgar( $entry, '1.6' ), 
        'message' => rgar( $entry, '3' ),
        );

    $request = new WP_Http();
    $response = $request->post( $post_url, array( 'body' => $body ) );

}

这会预先检查标题以确定您是否真的想 post 将您的信息提供给您的第三方 URL。虽然检查标题肯定会使您的代码更具可读性,但我实际上建议您检查表单的 ID 以获得更好的可靠性。 More information on Form Objects here

Forms 3rd party Integration Plugin 允许我设置多个第三方表单并与重力表单完全集成。

您可以将表单 ID 附加到操作名称以挂接到特定表单:

add_action( 'gform_after_submission_5', 'post_to_third_party', 10, 2 );

The documentation for gform_after_submission can be found here.