How do i schedule cron in wordpress for each second?

I want to schedule a cron job in Wp3.5.1 to sending email to those customer whose appointment is pending.

here some code am trying:

Read More

For the testing am scheduling corn for each second to insert row in table.

// insert row every second
add_action('wp', 'my_activation');
function my_activation() {
    if ( !wp_next_scheduled( 'my_event' ) ) {
        wp_schedule_event( time(), 'hourly', 'my_event');
    }
}

add_action('my_event', 'do_this_event');
function do_this_event() {
    global $wpdb;
    $wpdb->query("INSERT INTO `wordpress-testing`.`wp_test` (`id`, `text`) VALUES (NULL, 'b');");
}

wp_get_schedules('my_event');

//custom recurrence
add_filter( 'cron_schedules', 'cron_add_every_sec' );
function cron_add_every_sec( $schedules ) {
    $schedules['hourly'] = array(
        'interval' => 1,
        'display' => __( 'Do Secondly' )
    );
    return $schedules;
}

May am going wrong to do this. So please help me through.

Is cron run automatically after activation once OR its need to recurring visit after scheduled time past?

Related posts

Leave a Reply

1 comment

  1. I do not think it is useful to have a cron running every second, but the function itself is quite simple.

    You add a filter to the cron_schedules:

    function f711_add_seconds( $schedules ) {
        $schedules['everysecond'] = array(
            'interval' => 1,
            'display' => __('Every Second')
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'f711_add_seconds' ); 
    

    You can now use everysecond in your wp_schedule_event instead of the hourly.

    Note that the Cron is not executed every second, if nothing happens on your site, but it will be limited to max. one execution per second. Cron sets a value in your options-table for a timestamp when the function should be executed the next time. Then Cron checks if the time is in the future or in the past, and if it is in the past, the function is executed. The Cron runs automatically at this point but needs to be set again. So be sure to include the

    if ( !wp_next_sceduled('my_event') ) { /* scheduling code */ }
    

    in your code.

    For further information on how Cron works visit the Codex Page.

    You should not overwrite the hourly as you did, as some functions may use this schedule. Please be aware that some Plugins may clear your Cronschedule, if they are not coded properly (had a Caching Plugin like that once).