php – Scheduling an event inside plugin class is not working

Question

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 ) );
        
        return;
    }
}

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 ) );
OR
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?

Thanks!

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

Leave an answer

Browse
Browse