在Wordpress上发布保存后更改重定向URL

时间:2017-12-15 22:50:14

标签: php wordpress redirect curl advanced-custom-fields

我有一个问题,希望这里有一些好人可以帮助我。我准备了一个前端表格。这是一个捐赠表格,我已经在表格提交后添加了一个cURL电话到支付网关api。这里的问题是,我希望将表单页面重定向到支付网关api返回的URL,以便用户通过他们的银行在场外付款。

我的想法是在保存帖子之后但在用户被重定向之前更改帖子固定链接,以便将它们重定向到正确的URL(由支付网关api给出),而不是原始永久链接

有任何想法执行此操作吗?

到目前为止,这是我的代码:

<?php

add_action('acf/save_post', 'my_save_post', 10, 1);

function my_save_post( $post_id ) {

$billplzApi = get_field('billplz_secret_key', 'option');
$billplzId = get_field('billplz_collection_id', 'option');

// bail early if not a donation post
if( get_post_type($post_id) !== 'donation' ) {
    return;
}
// bail early if editing in admin
if( is_admin() ) {  
    return;
}

$post = get_post( $post_id);

$amount = '';
$donationGroup = get_field('donation_amount', $post);
$selectedAmount = $donationGroup['select_donation_amount'];
$customAmount = $donationGroup['specify_any_amount'];

if(!$customAmount){
    $amount = $selectedAmount;
} else {
    $amount = $customAmount;
}

$name = get_field('name', $post);
$email = get_field('email', $post);
$unmobile = get_field('mobile', $post);
$mobile = "+6" . $unmobile;


$billplz_data = array(
      'amount' => $amount * 100,
      'name' => $name,
      'mobile' => $mobile,
      'email' => $email,
      'collection_id' => $billplzId,
      'deliver' => false,
      'description' => 'a test for payment gateway',
      'redirect_url' => home_url('donation-redirect'),
      'callback_url' => home_url('donation-callback')
    );

      $process = curl_init('https://www.billplz.com/api/v3/bills/');
      curl_setopt($process, CURLOPT_HEADER, 0);
      curl_setopt($process, CURLOPT_USERPWD, $billplzApi . ":");
      curl_setopt($process, CURLOPT_TIMEOUT, 30);
      curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($process, CURLOPT_POSTFIELDS,http_build_query($billplz_data));
      $return = curl_exec($process);
      curl_close($process);
      $arr = json_decode($return, true);
      //this is the url to replace the original post permalink
      $billplz_url = $arr['url'];       

}

acf_form_head();
get_header();
?>

<div class="row">
    <div class="col-sm-12 col-md-8 col-md-offset-2 col-xs-12">

<?php 

while (have_posts()) : the_post();

acf_form(array(
    'post_id'       => 'new_post',
    'new_post'      => array(
                        'post_type'     => 'donation',
                        'post_status'   => 'publish'


    ),
    'submit_value'  => 'Donate',
    'html_submit_button' => '<input type="submit" class="acf-button btn btn-success btn-lg pull-right" value="%s" />',
    'return' => '%post_url%'
));

endwhile;

?>

    </div>
</div>

<?php 

get_footer();

?>

1 个答案:

答案 0 :(得分:1)

在此处找到此问题的解决方案:Create WordPress Page that redirects to another URL

基本上我需要添加:

header('Location:'.$billplz_url);
exit();

并删除:

'return' => '%post_url%'