0

I need to send a form submission to a specific endpoint but before I do that I want to do some custom handling on it. So I set up a custom handling function but now I can't figure out how to get the POST and forward it to the final endpoint.

Here is what I have:

<form class="" action="<?php echo admin_url('admin-post.php') ?>" method="post">
        <input type="hidden" name="action" value="bb_submit_email">
        <input type="email" id="email" name="email" value="example@email.com" required>
        <?php echo wp_nonce_field('submit-email', '_mynonce'); ?>
        <input type="submit" class="float-right btn btn-primary form-button" value="Submit">
    </form>
add_action('admin_post_bb_submit_email', 'bb_handle_email_submit');
add_action('admin_post_nopriv_bb_submit_email', 'bb_handle_email_submit');

function bb_handle_email_submit() {

    $logger = wc_get_logger();
         

    if ( !isset($_POST['_mynonce']) || !wp_verify_nonce($_POST['_mynonce'], 'submit-email')) {
      $logger->add('submit-email-debug', 'nonce failed!'); 

      return;
    }

When my custom handler is triggered, is there a way to get the post request and forward it to an external endpoint?

Dallin Davis
  • 421
  • 7
  • 18

2 Answers2

0

Did you try wp_redirect at the end of bb_handle_email_submit function?

function bb_handle_email_submit() {
   // Your script above 
   wp_redirect(home_url('/thanks'));
}
Argus Duong
  • 2,356
  • 1
  • 13
  • 24
0

You can make a HTTP POST request to your endpoint from your custom form handler. The article How do I send a POST request with PHP? describes how to make HTTP POST request.

The $_POST variable contains the contents of the form submission. You need to include this variable in your HTTP POST request to the remote endpoint.

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24