Asynchronous php code in WP Plugin Asynchronous php code in WP Plugin wordpress wordpress

Asynchronous php code in WP Plugin


Use Guzzle Package, code sample:

$request = $client->createRequest('GET', ['future' => true]);$client->send($request)->then(function ($response) {    echo 'Got a response! ' . $response;});

Look how can you install it. Also check the RingPHP and Future Responses for some additional information. Actually RingPHP is utilized as the handler layer in Guzzle and at the bottom, the React/Promise is giving the Promises/A support for PHP.


The proper way to process asynchronous requests in WordPress is to use WP-Cron to schedule an event. You can either schedule it to run once, or on an interval. See some guides on setting it up here. The two main functions to check out are wp_schedule_event() and wp_schedule_single_event().

One thing to keep in mind however is that because your code is only running when there is a request, if there is low traffic then it's possible that your scheduled event won't run when expected. I wrote an article on my site regarding how you can use crontab in conjunction with WP-Cron to more accurately schedule events: http://justinsilver.com/technology/wordpress/disable-wp-cron-wordpress/.