0

I trying to utilize wp_schedule_single_event to schedule an asynchronous task. However, my event was never called after 60 seconds. Do I need to do anything with ldp_new_topic_event hook before using it here? My site is running on a shared host.

Here is how I coded my schedule:

<?php

if ( !class_exists( 'LDP_Notifications' ) ) :

final class LDP_Notifications {

   public static function new_topic_notification( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false) {

       // Do some stuff.
       // Based on some conditions the following is called:
       error_log('++++ schedule an event. I see this printed.');

       add_action( 'ldp_new_topic_event', 'LDP_Notifications::send_push_notification', 10, 2 );
       wp_schedule_single_event( time() + 90, 'ldp_new_topic_event', array( $message, $deviceTokens ) );
   }

   public static function send_push_notification( $message, $deviceTokens )  
   {
        error_log('Expecting this after 60 seconds but I never got it.');
   }
}

endif;

?>

Thanks for your help.

  • Did you visit your site after 60 seconds had passed to allow cron to run? – czerspalace Nov 20 '15 at 18:01
  • I did go to my site after 60 seconds. However im not aware of settings i must config for scheduler to run. Where might i find the settings to allow the schedule? – Loc Pham Nov 20 '15 at 18:05
  • There are no settings, just that your will site only run any pending crons when someone visits the site (or if you have scheduled an external service to visit wp-cron ) – czerspalace Nov 20 '15 at 18:07
  • Ok. Yep i did revisit the site way after 60 seconds but the scheduled was never observed. Do i need to do anything special with my hook prior to this? Or does add_action took care of it already? – Loc Pham Nov 20 '15 at 18:12
  • Do you see your event when you _get_cron_array()? – jdm2112 Nov 20 '15 at 18:56
1

Your custom hook is never triggered because you are not subscribing to it the right way!

Context

Although you are calling it in your class function, it is never executed beyond that point.

Place the add_action function somewhere else like in a plugins_loaded hook. Put it outside your class where it can be seen.

Dependencies

Where are the variables $message and $deviceTokens declared? if they are not, the wp_schedule_single_event will fail! So no event is scheduled in the first place!

| improve this answer | |
  • Awesome, I got it to work by moving add_action to the plugins_loaded hook. It only works after the timer had expired and that I revisited the website. Isn't it suppose to fire automatically when the timer expires? Here's how Wordpress describes the schedule in two sentences: Schedules a hook which will be executed once by the WordPress actions core at a time which you specify. The action will fire off when someone visits your WordPress site, if the schedule time has passed. The first sentence give me an impression that it should fire without user intervention. – Loc Pham Nov 20 '15 at 22:40
  • Yes the first sentence gives that impression, but the second sentence takes it away. That's how threaded apps work. It's sad but it's true. Now if you feel my answer has worked for you, then mark it as so. This way others can identify the correct answer quickly. – Scriptonomy Nov 20 '15 at 22:55
  • Thanks. I didn't like the second sentence at all :) – Loc Pham Nov 20 '15 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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