0

I working on the wp_schedule_event recursive function. In the function, I have create a email message and it works. this function fires every 1 hour.

function do_this_hourly() {

    $to = '[email protected]';

    $subject = 'Test my one hour job';

    $message = 'you received this message ';

    mail( $to, $subject, $message, 'From: [email protected]' . "\r\n" );

}

but now, I have this external php page which needs to be refresh/load/reload every one hour without anyone loading it on the browser

$link = "http://www.example.com/path/to/link/"

header('Location: ' . $link);

Does anyone knows how to do it?

3
  • You can use server cron for it
    – Udit Rawat
    Commented May 18, 2017 at 13:15
  • I don't quite follow which one has to do with another. If it's external page, unrelated to your WP install, then how are WP bits relevant to it?
    – Rarst
    Commented May 18, 2017 at 13:37
  • Just know that a wp_schedule_event doesn't fire every on set intervals, in your case every hour. It fires everytime someone visits the website or wp-admin and then runs the function if at least the set interval has passed since the last visit. If no one visits your website for 24 hours, your code hasn't been executed either.
    – Jebble
    Commented May 23, 2017 at 8:58

1 Answer 1

0

I think if you need to call page every hour you can do it with a simple cron (not wp_cron) and set it in your panel or via shell a string like this:

  • */1 * * * wget -O - /path/to/link >/dev/null 2>&1

But if you need to do it via wp

add_action('wp', 'set_cron' );
add_action('test_scheduled', 'fn_test_scheduled');
function set_cron() {
    // Remove any scheduled event on this hook
    //wp_clear_scheduled_hook( 'exercise_reminder' );
    if (!wp_next_scheduled('test_scheduled')) {
        wp_schedule_event( gmmktime(8, 0, 0, 1, 4, 2015), 'hourly', 'test_scheduled');
    }
}
function test_scheduled() {
    $ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "/path/to/link/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
}

In this example you need to change gmmktime with your preference

Be careful with wp cron. I suggest you to use it only if it's a real cron https://tribulant.com/blog/wordpress/replace-wordpress-cron-with-real-cron-for-site-speed/

I hope this help you. David

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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