wp-cron event doesn't run when custom login API is enabled

by ellen   Last Updated September 11, 2019 07:08 AM - source

I have a cron job on my wordpress site. which I would like to run every hour. I also have a custom login function on my site: users are directed to my site via another site which passes login information via cookies.

When I get run of my custom login, the cron event runs fine. When I leave it in, the cron event just doesn't seem to run. I'm not sure how the two are related.

Here's how the cron event is set up (in functions.php):

add_action('init', 'my_activation');
add_action('myapp_update_user_scores', 'do_this_hourly');

function do_this_hourly(){
    smyapp_update_user_scores();
}

function my_activation() {
    if ( !wp_next_scheduled( 'myapp_update_user_scores' ) ) {
        wp_schedule_event(time(), 'hourly', 'myapp_update_user_scores');
    }
}

function myapp_update_user_scores(){
    //do stuff
}

And here's the login API stuff (also in functions.php):

add_action( 'init', 'check_login_status' );

function check_login_status() {
    $url = 'https://endpoint';

    //get cookie to send with API call
    $headerCookies = explode('; ', getallheaders()['Cookie']);
    $cookies = array();
    foreach($headerCookies as $itm) {
          list($key, $val) = explode('=', $itm,2);
          $cookies[$key] = $val;
    }
    $args = array(
        'cookies'=> $cookies
    );
    $api_request = wp_remote_get($url, $args);

    if ( ! is_wp_error( $api_request ) ) {
        //do stuff 
    }else{
       //log the error and redirect them away from the site
    }
  }



Related Questions


Need to execute a cron job

Updated June 30, 2015 13:03 PM

Cron job cannot use "wp_remote_post"

Updated October 30, 2018 07:08 AM

WP_CRON issue with UTC and local time

Updated November 29, 2018 01:08 AM

WP_cron not updating usermeta in plugin

Updated December 05, 2018 06:08 AM