plugins - How to use wp-ajax in wp-cron

admin2025-01-08  4

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

Share Improve this question asked Apr 16, 2018 at 10:19 John478John478 11 bronze badge 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265785a1070.html

最新回复(0)