Home > Net >  Create custom scheduled with desired time in WordPress
Create custom scheduled with desired time in WordPress

Time:10-31

I have written my own schedule and it is set up fine. But I want my schedule code to run every 6 hours. I have put six_hours but it doesn't work. Thanks for helping me?

this is my code :

add_action('init', function() { 
    if (!   wp_next_scheduled( 'awpl2_cron_hook2' ) ) {
        wp_schedule_event( time(), 'six_hours', 'awpl2_cron_hook2' );
    }

});
add_action( 'awpl2_cron_hook2', 'awpl2_cron_function2' );
function awpl2_cron_function2() {
    send_request($client . "&url_domainsss=$url&email_domainsss=$email");
}

I want my scheduler code to run every 6 hours but it doesn't happen. But my scheduler code works fine and runs.

CodePudding user response:

I have written a code for you to set the timing to any value you want. Just put it in seconds.

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


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

});

function prefix_function_callback() {
    //your code here
    var_dump('run is');
}
add_action( 'my_custom_scheduled', 'prefix_function_callback' );

  • Related