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?
Regards
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?
Regards
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
wp_schedule_event()
to dail run function clean_my_db()
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.clean_my_db_table('posts')
runsclean_my_db_table('postemta')
runsTo 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.