php – Scheduling an event inside plugin class is not working


wp_schedule_single_event is not firing.
I wrote a function that is posting to external API, if it failed I schedule the same posting function to run 5 seconds later in cases of timeouts.
Here’s the simplified code of the function:

class MyPlugin {
    public function post( $fields, $type ) {
        // Posting to external API logic, if it failed I schedule the same function to run in 5 seconds.
        wp_schedule_single_event( time() + 5, 'post', array( $fields, $type ) );

I assumed that the post function is simply not found so when the time comes for that function to run, nothing happens, so I tried to use:
wp_schedule_single_event( time() + 5, MyPlugin->post(), array( $fields, $type ) );
wp_schedule_single_event( time() + 5, 'MyPlugin->post', array( $fields, $type ) );

Nothing seems to work! How can I get the same function to run with the same accepted args, 5 seconds after it failed?


odedta 3 months 2022-04-27T06:19:29-05:00 0 Answers 0 views 0

Leave an answer