Gab Comtois
Gab Comtois

Reputation: 11

Wordpress Cron job not running

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.

Upvotes: 1

Views: 3455

Answers (1)

B. Fleming
B. Fleming

Reputation: 7220

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.

Upvotes: 1

Related Questions