0

When submitting a form I want to call an additional function that will connect to a webservice. Because of the slow response time of that webservice, I don't want the page to keep loading untill the process is done.

Instead the action should be fired/called and run on the background.

What is the best way to achieve this?

  • 1
    You may use ajax for this.and in the ajax page, if is just need to be execute and no need to provide data, then you can use ignore_user_abort function.Thus if browser close the connection to the ajax page, still the ajax will execute – Abhisek Malakar Mar 4 '15 at 13:01
  • Is there a way to not use ajax? I need to call this function in a Gravity Forms callback 'after_post_submission'. – Jeffrey van Rossum Mar 4 '15 at 13:49
  • This would be a perfect use case for a "promise" (in PHP, when you got a meaningful architecture - which WP is not - this would be what happen if a "subject" triggers it's "observers" on change of state). Point is, that for you there's pretty much no way around an async AJAX call that utilizes a JS promise to not block the rendering cycle. I'll leave that question open, but you should rework it massively after you have started working on your AJAX call(backs) and the JS part. – kaiser Mar 11 '15 at 2:15
1

Use the WordPress HTTP API with a low timeout & blocking disabled. It's what WordPress core does to spawn the cron API:

wp_remote_get(
    $url,
    array(
        'timeout'   => 0.01,
        'blocking'  => false,
        'sslverify' => false,
    )
);
  • I now use the wp schedule single event function. Would you suggest wp_remote_get to be the better solution? – Jeffrey van Rossum Mar 6 '15 at 8:08
  • Depends how immediate you need to fire the request. – TheDeadMedic Mar 6 '15 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.