I have create a custom wordpress plugin where i am using wp ajax. In class's __construct i have an action (admin_footer) in order to use wp_enqueue_script and wp_localize_script following wp instructions

So the flow is something like php -> js -> and again php

Why I am doing that ? To avoid memory limit error. Here says something nice : "The solution above routes around PHP’s limitations by breaking a single large task into a number of smaller"

Everything works like a charm in backend!

My question is how i can manage this process to work with wp-cron? Is possible for wp-cron to work with js?

I tried wp_schedule_event with my custom hook without success (js never works).

Any ideas?


  • What you want to do cannot be done in a single request, even if that request then makes lots of other requests, be they AJAX requests or any other kind. Unless the entire process can be done in the time allotted, the maximum time will be reached. You need to break it apart into smaller chunks that can be processed. This approach will never work from a single WP Cron event, this is a dead end
    – Tom J Nowell
    Commented Jan 29, 2021 at 1:07

1 Answer 1


First of all, think about using the new REST API instead of Admin-AJAX in your environment where JavaScript is available.

Now to answer your question: JavaScript isn't available for wp-cron, as these requested solely run on the server and not on some interpreted HTML/JavaScript. So what can you do? Well, just schedule another event if you didn't finish yet (or your separated workload is done).

While wp_schedule_event() is used for recurring events, there is also wp_schedule_single_event() which can be used for the "not yet finished" workloads.

Say you want to clean your db daily, a workflow could look like this

  1. wp_schedule_event() to dail run function clean_my_db()
  2. Within clean_my_db() you create an array $tables_to_clean = ['posts', 'postmeta']. Now you call wp_schedule_single_event() to run clean_my_db_table() and pass each of the tables as argument.
  3. clean_my_db_table('posts') runs
  4. clean_my_db_table('postemta') runs
  5. etc

To summarize: You have one function called by wp_schedule_event() which is a recurring event. Within that you decide to split workloads and run them via wp_schedule_single_event() at specific dates/times.

  • Hi, i start this plugin using WooCommerce REST API but it was too slow for what I want (sync thousand items from other system ), so i change it to WC_ class. Using new REST API instead of Admin-AJAX how i can avoid php time limit when I'll schedule me job (e.g. sync 10.000 items from other system to wp) ?
    – John478
    Commented Apr 24, 2018 at 8:36
  • @John478 The answer stays the same: don't sync 10.000 at once, but in small batches over time
    – kero
    Commented Apr 24, 2018 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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