2

I don't know why my scheduled cron events not working anymore, it used to before with the same code. I wrote a plugin that updates a CRM with the order note, if the communication fails I setup a scheduled event for the same function to run with the same arguments for 1 minute in the future with this piece of code:
wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'woo_kala_order_note_added', array( $comment_id, $kala_order_id ) );
I tried to place woo_kala_order_note_added function both in the plugin and in the theme and error log the arguments but my debug.log is still empty.

Here's a screenshot of the Cron Manager plugin showing the schedule that is registered but the function won't actually fire. enter image description here

Any idea why this might happen?

Thanks

5
  • Are other events working? How are you triggering cron - the default way, or with a scheduler? Have you got define( 'DISABLE_WP_CRON', true ); ? If you visit / curl /wp-cron.php does that trigger the job?
    – Rup
    Commented Jun 4, 2023 at 10:01
  • Hey @Rup I'm using Wordpress Cron Scheduler, all native Wordpress functions. DISABLE_WP_CRON is not set. When I look at the cron events, I see that the action is registered and when the time passes it disappears so the cron systems looks to be working well, it's just that the function is never fired because I placed error_log at the beginning of it. Vising /wp-cron.php doesn't really help.
    – odedta
    Commented Jun 5, 2023 at 8:20
  • Which web hosting service are you using for your website ?
    – PhpDoe
    Commented Jun 7, 2023 at 11:55
  • 1
    I'd probably stick some error_log() calls into wp-cron.php then to work out what it's doing. Going to /wp-cron.php should process any queued jobs. Also check if there are any transients in your option table for cron e.g. _site_transient_doing_cron or _transient_doing_cron (I can't remember which) and any other CRON related config in wp-config e.g. ALTERNATE_WP_CRON.
    – Rup
    Commented Jun 7, 2023 at 12:50
  • While you're using error_log are you absolutely sure you're checking the correct location for your erorr_log files? Depending on your configuration, the particularly logs you're looking for may be output to a location you're not aware of - this is somewhat common. The location can also change depending on the what is called and when.
    – Paul G.
    Commented Jun 8, 2023 at 10:16

2 Answers 2

1

You just need to hook your function to the hook you named in the second parameter of wp_schedule_single_event function.

See the following example:

add_action('woo_kala_order_note_added','woo_kala_order_note', 10, 2);
function woo_kala_order_note( $comment_id, $kala_order_id ){
    // do whatever your logic needs. 
}
1

Make sure the second argument in wp_schedule_single_event( $timestamp, $hook, $args, $wp_error ) is referring to a "hook name" and not a function name.

function custom_event_callback()
{
    error_log( "Hello world!" );
}
add_action( "mycustom_event_hook", "custom_event_callback" );

function schedule_custom_events()
{
   wp_schedule_single_event(
      time() + MINUTE_IN_SECONDS,
      "mycustom_event_hook",
      [ $comment_id, $kala_order_id ]
   );
}
add_action( "init", "schedule_custom_events" );

Hope this helps.

2
  • 1
    D'oh yes I missed that. Good spot! You can add_action the handler at the top level though: you don't need to wait for init.
    – Rup
    Commented Jun 12, 2023 at 10:43
  • No need to add the action "mycustom_event_hook" inside "init" hook, it will be triggered by the "wp_schedule" itself
    – Ahmad Wael
    Commented Jun 12, 2023 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.