Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to set up a scheduled task in my Wordpress installation which will (eventually) run a script to send an email once a day with a load of data in.

However I can't seem to get a scheduled task to fire at all. I am testing it with a 2 minute interval to see if I can get it working. What I have so far is below, and is at the bottom of my functions.php file. Any idea where I'm going wrong?

add_filter('cron_schedules','my_cron_definer');
function my_cron_definer($schedules){
    $schedules['twomin'] = array(
        'interval'=> 120,
        'display'=>  __('Once Every 2 Minutes')
    );
    return $schedules;
}

add_action('my_periodic_action','my_periodic_function');
function my_periodic_function(){
    mail('email@address.co.uk','Test!', 'Test Message');
}
wp_schedule_event(time(), 'twomin', 'my_periodic_action');

I'm aware my Wordpress site needs to be getting it's pages visited for the function to run, so I have been clicking around on the site hoping to trigger my function but no luck!

Note: I have swapped my real email address out!

share|improve this question
add comment

1 Answer

You have to schedule your event in a hook, for example in after_setup_theme or wp actions:

add_filter('cron_schedules','my_cron_definer');
function my_cron_definer($schedules){
    $schedules['twomin'] = array(
        'interval'=> 120,
        'display'=>  __('Once Every 2 Minutes')
    );
    return $schedules;
}

add_action('my_periodic_action','my_periodic_function');
function my_periodic_function(){
    mail('email@address.co.uk','Test!', 'Test Message');
}

add_action( 'wp', 'wpse8170_setup_events' );
// or add_action( 'after_setup_theme', 'wpse8170_setup_events' );
function wpse8170_setup_events() {
    if ( !wp_next_scheduled( 'my_periodic_action' ) ) {
        wp_schedule_event(time(), 'twomin', 'my_periodic_action');
    }
}
share|improve this answer
    
Hi, thanks for your answer. I have given your example a go but I'm still having no luck. Is there anything else you're aware of (not necessarily related to the above code) that could be preventing it from working? –  Probocop Jun 12 '12 at 11:06
    
@Probocop it's very strange... try to display something in your my_periodic_function call to check if it works. If it works, then mail function is not working. –  Eugene Manuilov Jun 12 '12 at 11:45
    
I've got a test script written (which will eventually be the scheduled script) that has the wp_mail (and has had mail) function within which works if I manually run the script. Unless there is a reason why the mail functions wouldn't work from within functions.php? –  Probocop Jun 12 '12 at 13:13
    
While I'd still like to know how to do this, I've abandoned this approach in favour of using a scheduled task instead (site is in IIS). Thanks! –  Probocop Jun 13 '12 at 13:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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