Developer Snippet Diary

wordpress cron jobs

Step 1: Create the Function (The Task)

function my_custom_cron_job() {
    $to = "rizikmw@gmail.com";
    $subject = 'WordPress Cron Job Ran';
    $message = 'This is a test to confirm your cron job is working.';
    wp_mail( $to, $subject, $message );
}

Step 2: Schedule the Event

// Schedule the event upon plugin activation or theme initialization
function schedule_my_cron() {
    if ( ! wp_next_scheduled( 'my_custom_cron_hook' ) ) {
        // Schedule the event to run once daily hourly, twicedaily, daily
        wp_schedule_event( time(), 'daily', 'my_custom_cron_hook' ); //Timestamp: When to run first (usually time() for now).
    }
}
add_action( 'wp', 'schedule_my_cron' );

Step 3: Hook the Function to the Event

add_action( 'my_custom_cron_hook', 'my_custom_cron_job' );

 

Posted by: R GONDAL
Email: rizikmw@gmail.com