How to create cron event in wordpress without plugin

 

function myprefix_custom_cron_schedule( $schedules ) {
    $schedules['every_six_hours'] = array(
        'interval' => 2 * 60, // Every 6 hours
        'display'  => __( 'Every 6 hours' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );

//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
    wp_schedule_event( time(), 'every_six_hours', 'myprefix_cron_hook' );
}

///Hook into that action that'll fire every six hours
 add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );

//create your function, that runs on cron
function myprefix_cron_function() {
    add_option('test_world', 'Hello world');
    add_option('make_world', 'This is second world');
    add_option('make_world 3', 'This is second world 2');
    add_option('make_world 4', 'This is second world 4');
}

 

Credit: Click Here For Solution




wp_schedule_event() in plugin not scheduling a cron event



Post a Comment

Previous Post Next Post