将自定义数据发送到自定义确认对象 - 重力形式



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

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 ) );
}

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

当我将数据发送给第三方时,在过滤器操作结束之前会发回响应,我想将该数据传递给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>";
}

相关内容

  • 没有找到相关文章

最新更新