Peter Peter - 2 months ago 10
PHP Question

Wordpress cronjob every 3 minutes

there are many post on stackoverflow with this topic but (i dont know why) nothing will work for me.

What i have

function isa_add_every_three_minutes( $schedules ) {

$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __( 'Every 3 Minutes', 'textdomain' )
);

return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


function every_three_minutes_event_func()
{
// do something
}

wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );


Any ideas what i'm doing wrong?

Answer

You need to use add_action() to hook your function to the scheduled event.

add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

Here is the full code.

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

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

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    // do something
}
?>