0

Initial scenario: I have PHP scripts in Wordpress that stores the entries of a multi step form post in MySQL DB and using PHPMailer emails the form entries to multiple emails (retrieved from DB and set in an array) in a loop.

Issue: The form post takes time to complete as it stores the entries in DB and sends out emails to multiple addresses. As it takes much time (more than 1min) for the form post, user would quit the form, affecting the lead conversion.

Proposed design: After form post, just store the form's entries in the DB table and so that form submission is completed, dispatch the function for email submissions in a loop till later using scheduled task or cron job. I imagine this reduces form submission time as the email submissions will just be a background process.

If this is a good design, can I use cron job? I was thinking of using https://www.sitepoint.com/introducing-cron/ But unsure how I can actually trigger this after a form submission is done. In below code snippet I have a function which handles what happens after form submission but only after send_notification is completed/returned the form submission is complete.

add_action('gform_after_submission_19', 'send_custom_notification', 10, 2);

function send_custom_notification($entry, $form) {
    send_notification("form_submission", $form, $entry);
    /*Is there a way to dispatch send_notification as a schedule task 
     or cron job so that form submission is finished but notification 
    is background process?*/
}

Instead I need a way to trigger the send_notification after form completion. Can one dispatch or trigger cron job or scheduled task after a function is completed?

Qwerty
  • 323
  • 1
  • 6
  • 33

1 Answers1

1

You use asynchronous function calls, or processing queues. Laravel has something like this built-in.

I recommend the second method because of the many benefits it provides like scalability and manageability.

The basic idea is, you send command into a queue and essentially forget about it. You have queue job workers running somewhere else that will take jobs from the queue and process it. That way your original process -- the one that put jobs in the queue -- can resume its processing without waiting for those tasks to finish.

aljo f
  • 2,430
  • 20
  • 22
  • Right that makes more sense, thanks! Was wondering if I can incorporate RabbitMQ or Laravel to Wordpress though, is that recommended? Would it be better to implement Laravel to my existing WP site? I don't need Heroku as I'm not using a cloud PaaS, just hosting service like Siteground. I sort of wish I made the full site in Laravel now, but late now as I'm already using WP, definitely for next project. – Qwerty Jun 03 '17 at 04:40
  • Looks like I can use Laravel with WP, http://rundef.com/using-laravel-within-wordpress-backend. I think I will go with Laravel with WP for its obvious benefits, I will try queueing mail with Laravel then. – Qwerty Jun 03 '17 at 04:51