I am working on WordPress plugin and I have added support for wp cron there. See the code below:
function __construct(){
//Cron Job
add_action('init', array($this,'g_order_sync'));
add_action('ga_order_syn', array($this,'sync_order'));
add_filter('cron_schedules',array($this,'my_cron_schedules'));
}
function my_cron_schedules($schedules){
if(!isset($schedules["10sec"])){
$schedules["10sec"] = array(
'interval' => 10,
'display' => __('Once every 5 minutes'));
}
return $schedules;
}
public function g_order_sync(){
if( !wp_next_scheduled('ga_order_syn') ) {
wp_schedule_event( time(), '10sec', 'ga_order_syn' );
}
}
public function sync_order(){
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT']. "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
}
When I print cron using print_r( _get_cron_array() );
, it shows me that my cron(ga_order_syn
) is scheduled every 10 second but sync_order()
function does not create file at DOCUMENT_ROOT
. If I add a call to the wp_mail()
function, it does not send me an mail.
What is issue with my code? Why is it not working?