将自定义数据发送到自定义确认对象 - Gravity Forms

Sending custom data to custom confirmation object - Gravity Forms

目前我通过使用类似的代码结构发布到第 3 方来进一步增强重力形式的功能:

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

    $endpoint_url = 'https://thirdparty.com';
    $body = array(
        'first_name' => rgar( $entry, '1.3' ),
        'last_name' => rgar( $entry, '1.6' ),
        'message' => rgar( $entry, '3' ),
        );
    GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );

    $response = wp_remote_post( $endpoint_url, array( 'body' => $body ) );
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}

我知道您可以注入各种挂钩点,但我遇到了自定义确认问题。我可以创建过滤器并进行自定义确认,但我希望它是动态的。

当我将数据发送给第 3 方时,在过滤操作结束之前会发回响应,我想将该数据传递给 custom_confirmation 挂钩。我怎样才能做到这一点?有没有办法操纵从 after_form_submission 挂钩传递的 $form 或 $entry 变量?

add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
    if( $form['id'] == '101' && $form['success'] == 1 ) {
        $confirmation = array( 'redirect' => 'http://www.google.com' );
    } elseif( $form['id'] == '101' && $form['success'] == 0) {
        $confirmation = "failed";
    }
    return $confirmation;
}

是否可以像我上面发布的那样?

我不得不通过回显 after_submission 挂钩底部的脚本来执行一个非常丑陋的解决方法,该脚本将确认消息保存到变量中。然后将该变量传递到浏览器的 localSession 中。然后在加载页面并向用户显示消息时拾取它。

if (is_numeric($RESPONSE_VAR)) {
    $msg = "<h2>Your registration was successful</h2><br><h2>Thanks for contacting us! Check your email, and activate your account..</h2>";
    echo "<script>localSession.removeItem('confirmation'); localStorage.setItem('confirmation', '$msg');</script>";
}else{
    $msg = "<h2>Sorry something went wrong with your application, please try again. <a href='https://URL/FORM_NAME/'>Go Back</a></h2>";
    echo "<script>localSession.removeItem('confirmation'); localStorage.setItem('confirmation', '$msg');</script>";
}