I want to do it in the background immediately after posting a Wordpress article (parallel?)

Asked 2 years ago, Updated 2 years ago, 89 views

I'd like to do it in the background right after posting the article on Wordpress.
Apparently, the wp_schedule_single_event has not been triggered properly.
I would appreciate it if you could point out what is wrong with the code below!

// functions.php

add_action('save_post', 'registerMyCron');

function registerMyCron(){
  wp_schedule_single_event(time(), 'myCronAction');
}

add_action('myCronAction', 'doSomething');

function doSomething() {
  // Some sort of action...
}

php wordpress

2022-09-30 10:47

1 Answers

The WordPress Codex describes wp_schedule_single_event():

Registers a hook (action) that the WordPress core function performs only once at a specified time.The action will take place after the scheduled time when someone visits the WordPress site.

Therefore, specifying the current time +n seconds does not guarantee that it will run at that time, and it does not run immediately after specifying the current time.

By calling the spawn_cron() function, the schedule can be checked and executed immediately, so if you call it, it can be executed immediately.

function registerMyCron(){
  wp_schedule_single_event(time(), 'myCronAction');
  spawn_cron();
}

I'm not familiar with WordPress myself, so I don't know if this method is appropriate.

Note How to perform heavy WordPress plug-in tasks asynchronously in the background|Slowly…


2022-09-30 10:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.