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 have written a plugin that will convert posts to excel format and then email the .xls to an email id. I can make it work with when function is declared in functions.php but does not work with function defined in plugin file.

if ( ! wp_next_scheduled( 'xls_func_hook1' ) ) {
  wp_schedule_event( time(), 'hourly', 'xls_func_hook1' );
}

add_action( 'xls_func_hook1', 'sendxls1' ); //senxls1 is a function in functions.php
if ( ! wp_next_scheduled( 'xls_func_hook2' ) ) {

  wp_schedule_event( time(), 'hourly', 'xls_func_hook2' );
}
add_action( 'xls_func_hook2', 'export2excel' );

The full code is at Here

share|improve this question
    
when you say not working ... what do you mean? Have you read that wp-cron does not always run exactly as you plan it –  Damien Aug 9 '12 at 18:17

2 Answers 2

@Chris_O is half-right and your code is half-right, see this documentation. On plugin activation, you register a custom action hook (so that your plugin doesn't repeatedly register duplicate events). Then you attach your cron function to that custom hook.

share|improve this answer
1  
IMO, would be better if you reproduced the code from the Codex in your Answer. –  brasofilo Feb 22 '13 at 8:40
    
@brasofilo And what happens when WordPress updates their documentation? Yet more stale information. –  Walf Mar 4 '13 at 3:08
1  
It's not "their" documentation, it's ours. And you are making a statement based on the current state of affairs, if there's an update in the docs, your Answer won't make sense anymore. ::: Anyway, it's just my opinion. –  brasofilo Mar 4 '13 at 12:54
    
It is "theirs" as in the group of active wordpess doc maintainers. That you are one of them does not negate that. I think linking to specific doc page will remain more salient than duplicated code ever will, even if that function is deprecated. There are thousands of sites out there that continue to perpetuate old hacks and misguided advice. I don't think stack exchange sites should be amongst them. –  Walf Mar 8 '13 at 0:35
    
Walf Sorry, but @brasofilo is right on that. SE policy is to keep everything in questions and answers to prevent link rot. Later users can (and will) improve your answer if it gets outdated. –  kaiser Oct 3 '13 at 2:39

The cron event needs to be registered on the plugin activation hook like so:

register_activation_hook( __FILE__, 'activate_cron' );
function activate_cron() {
    wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'the_function_to_run' );
}

The the_function_to_run defined in the wp_schedule_event needs to be your function that you want to run at the time interval.

Note: When you say something is "Not Working" it is really hard to answer. Please be more specific to get better answers.

share|improve this answer
    
i added this as you mentioned register_activation_hook( FILE, 'activate_cron' ); function activate_cron() { wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'export2excel' ); } . By not working i mean that it does not send an email. @Daimen can you please elaborate how it doesn;t work the way we want it to work. –  Amna Ahmed Aug 9 '12 at 18:30
    
If i register the cron in plugin activation hook, then it will run once atleast when the plugin is activated after deactivation? –  Amna Ahmed Aug 9 '12 at 18:47
    
WP-Cron is not the same as server cron. You can have a schedule, but wp-cron needs to be triggered by someone visiting your site. As the WP-Cron function only runs when there is a page request see also wp.tutsplus.com/articles/… –  Damien Aug 9 '12 at 19:14
    
Does that mean if a wp_cron is scheduled after every hour, it actually counts 60 visits and then runs? @Damien –  Amna Ahmed Aug 9 '12 at 20:05
    
No it just means that if no one visits your site for 3 hours then your cron will have missed running 3 times and will not run until someone visits your site. Basically WordPress does not load anything unless it gets a visit. –  Chris_O Aug 9 '12 at 20:08

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.