Cron Job not working in plugin

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

Related posts

Leave a Reply

2 comments

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

  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.