1

I used wp_schedule_event() to schedule a task to run hourly on my website. For some reason, the function i want to run is working, the event itself appear under the 'cron' option table. Everything seems fine but when i wait and check if the functions did run i have nothing.

i use hourly for testing but it's meant to be daily

Here is the full snippet of php code :

/*
======================================================
  MANAGE SUB CANCELLATIONS
  1 - daily event to run
  2 - manage_sub_cancel()
======================================================
*/


/* *   
   *
   * -- > 1 - daily event to run
   *
   * */


add_action( 'wp', 'check_sub_activation' );
function check_sub_activation() {
  if ( ! wp_next_scheduled( 'check_sub_event' )) {
    wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'check_sub_event' ); // creating the hook
  }
}

// add_action( 'wp', 'check_sub_deactivation');
function check_sub_deactivation() { // will clear the scheduled hook if activated
    wp_clear_scheduled_hook('check_sub_event');
}


/* *   
   *
   * -- > 2 - manage_sub_cancel()
   *
   * */


function manage_sub_cancel() { // This function will apply the cancellation of a boutique when period end is reached
  $args = array(
    'post_type'     => 'boutique',
    'meta_key'      => 'cancel_at_end',
    'meta_value'    => 'true',
    'meta_compare'  => '=',
  );

  $the_query          = new WP_Query( $args );
  $boutique_list      = $the_query->posts;

  foreach ( $boutique_list as $boutique ) {
    $boutique_id    = $boutique->ID;
    $period_end     = get_post_meta( $boutique_id, 'period_end', true );
    $today          = time();

    if ( $today < $period_end ) { // À INVERSER LORSQUE TERMINÉ
      update_post_meta( $boutique_id, 'sub_id', "" );
      update_post_meta( $boutique_id, 'pckg_id', "1" );
      update_post_meta( $boutique_id, 'cancel_at_end', "false" );
      update_post_meta( $boutique_id, 'period_end', "" );
    } else {
      // Period end is not passed
    }

  }
}

add_action( 'check_sub_event', 'manage_sub_cancel' ); // add manage_sub_cancel() function to 'check_sub_event' hook we just created

I'm also using the plugin Advanced Cron Manager to list all cron jobs, i do see the one i created here, i can also "execute now" and it's working.. but when i wait for the even to run every hour i don't see anything even if i visit the website. I don't know it seems like everything is done fine but nothing happen.

1 Answers1

1

WordPress cron jobs aren't actual cron jobs. WordPress will check if the job has run when the script is next executed, i.e. when a user visits a page that causes the script to run, and will then run the cron job if needed. If no one loads a webpage, then the script will not run and therefore your cron job won't run, either.

You only have a few options available to you for getting around this limitation:

  1. If you have root access to your hosting environment, then create an actual cron job to trigger a page load periodically (e.g. once every ten minutes).
  2. If you don't have root access, then setup your own personal server and do #1 on this new server instead.
  3. Leave your PC on with a script running--or a webpage open that runs some JavaScript--to periodically send a request to your website.

Those are listed in order of preference. #1 is best, #2 is more expensive and slightly more complicated but still a somewhat reasonable option, and #3 is pretty terrible and unreliable but could work in a pinch.

B. Fleming
  • 7,170
  • 1
  • 18
  • 36
  • I do not mind waiting for a visitor to load the website, thing is it's not working at all. I'm working everyday on my website i load pages 5-10 times every single hour but the event is just not triggering. As i said, if i use "execute now" on my plugin, it's working so the function is working and the action is also fired on this hook but the "hourly" schedule isn't running even if it's supposed to if i check wp_next_schedule('my_hook) it is there. I don't know if im saying right do you understand my problem? – Gab Comtois Nov 18 '19 at 20:03
  • It's supposed to fire a function every hour, if i just wait an hour and refresh it's not working. But if i execute now in my plugin (skip the hour wait) and refresh it's working. – Gab Comtois Nov 18 '19 at 20:05
  • @GabComtois Within `check_sub_activation()`, log a message at the very start of the function call. Similarly, log a message at the very start of `manage_sub_cancel()`. Check these logged messages to see if either of them are not executing when expected. You might also log the result of `wp_next_scheduled( 'check_sub_event' )` to see when the next event is scheduled. – B. Fleming Nov 18 '19 at 21:56
  • Knowing where the failure is occurring is important here. For instance, if the event scheduling check is never being called, then the `wp` hook isn't triggering it correctly. If the scheduled event time is way in the future, then maybe you messed with the interval during testing and never unscheduled the test event, resulting in the event never triggering in a timely fashion. If `manage_sub_cancel()` isn't being called, then event hook for that might not be functioning correctly, but if it is being called, then maybe there's something else wrong with your code entirely. – B. Fleming Nov 18 '19 at 21:59