Run a php file daily at specific time

I’m trying to add a cron job to run file daily at 7pm.

How can i add the file run command and specify to run it at 7pm ?

// Scheduled Action Hook
function run_my_script( ) {
// run my file : mysite.com/cron.php
}
// Schedule Cron Job Event
function USERS_MONITORING() {
if ( ! wp_next_scheduled( 'USERS_MONITORING' ) ) {
    wp_schedule_event( time(), 'daily', 'USERS_MONITORING' );
}
}
add_action( 'wp', 'USERS_MONITORING' );

I don’t know if there is a better solution.

1 Answer
1

You can include the PHP file and do the tasks, if WP-cron is your only option.

// Scheduled Action Hook
function run_my_script( ) {
    require_once('related/path/to/php/file.php');
}
// Schedule Cron Job Event
function USERS_MONITORING() {
    if ( ! wp_next_scheduled( 'USERS_MONITORING' ) ) {
        wp_schedule_event( strtotime('07:00:00'), 'daily', 'USERS_MONITORING' );
    }
}
add_action( 'USERS_MONITORING', 'run_my_script' );

Note that you need to include the related path. If you want to access the PHP file by its URL, you need to use cURL instead.

Also, as @rarst mentioned in one of his posts:

Note : WP Cron isn’t guaranteed to run at precise time since it is
trigerred by visits to the site. I am not confident if recurrent runs
will “stick” at midnight or will slowly slip from there, you might
need to readjust periodically.

Leave a Comment