0

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(){
    myapp_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
    }
  }

  • Maybe you need an exception in your login code for if (wp_doing_cron()) ? – Rup Sep 11 at 9:31
  • @Rup that sounds like a good idea, I just tried it however and doesn't seem to work. Could it have something to do with the 'init'? maybe I should be using something other than 'init'. Not sure – ellen Sep 11 at 22:11
  • @ellen have you used a tool like WP Control to verify that your function is not scheduled to run? – Mike Baxter Sep 13 at 21:31
  • Not sure if the sample code you provided is EXACTLY what you have on the server, but I noted your code has a typographical error that would prevent execution. Inside function do_hourly(){} you call smyapp_update_user_scores(), an erroneous reference to myapp_update_user_scores(); – Mike Baxter Sep 13 at 22:04
  • @MikeBaxter sorry, that type isn't in my actual code. It's fixed now – ellen Oct 15 at 4:31

Your Answer

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

Browse other questions tagged or ask your own question.