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:

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');");


//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?

  • 1
    cron running at every second is going to massively slow down your site on shared hosting, and at the same time be extremely unreliable ( you'd need a visitor a second or more for it to work ) and if your cron task takes longer than a second to execute, the next one might not happen – Tom J Nowell Feb 26 '13 at 9:50
  • This is only for testing on local-host to achieve the working behind 'wp_schedule_event()' and recurrence customization. Not for the live site i know its not make sense. – Frank Feb 26 '13 at 10:44

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).

| improve this answer | |
  • Thanks for great knowledge. Thatz means i needs to hit site in every second to run scheduled cron. wp_schedule_event( time(), 'everysecond', 'my_hourly_event'); – Frank Feb 26 '13 at 10:44
  • Yep, that's exactly what that means :) If you really depend on a Script being executed every second (I can't think of a case where this is necessary when nobody visits the site) you should go for a real Cronjob instead of wp_cron. – fischi Feb 26 '13 at 11:05
  • I have already second option, set corn job on server. I tried above custom recurrence everysecond but its not working though on local-host. – Frank Feb 26 '13 at 11:24
  • Yeah, that's what i expected - because on your localhost it will not often happen that you load your page every second ;) – fischi Feb 26 '13 at 11:26
  • Do you know the reason? Why? – Frank Feb 26 '13 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.