1

I'm trying to execute a piece of code asynchronous. I tried to use wp_schedule_single_event like this in my WP plugin:

function my_async_test(){
$fp = fopen(wp_upload_dir()['basedir'] . '/log.txt', 'w');
fwrite($fp, 'here1');
fclose($fp);
}

add_action('my_async_test_hook','my_async_test');

...

//ajax handler
function my_ajax_handler(){
//stuff
wp_schedule_single_event( time() + 120, 'my_async_test_hook' );
}
add_action( 'wp_ajax_nopriv_my_ajax_handler', 'my_ajax_handler' );

When I call the ajax handler the event seems to be scheduled correctly. I have a cron plugin showing me that the event is correctly scheduled to run after 2 minutes. When the time is up, the event disappears (probably it gets triggered), but the code in my_async_test doesn't seem to be executed.

If instead of the wp_schedule_single_event I directly call the function my_async_test(), of course, it works.

What am I missing. How can I run my_async_test without blocking the ajax function

thanks!

  • It looks like wp_schedule_single_event is not working as expected if called from within an action – coding-dude.com Sep 18 at 13:44

Your Answer

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

Browse other questions tagged or ask your own question.