I’m trying to implement a cron job in my wordpress blog. I want to do this stuff in a plugin, for testing I’m trying to write in one file some log information every 10 minutes, for do that I wrote this code (PHP):
add_filter( 'cron_schedules', 'ten_minute_prefix' );
function ten_minute_prefix( $schedules )
{
$schedules['tenmins'] = array(
'interval' => 600,
'display' => __( '10 minutes' ),
);
return $schedules;
}
//This must be here always
add_action('my_task_hook', 'foo_task');
function foo_task()
{
file_put_contents('data.txt', date("Y-m-d H:i:s") . "task do itrn", FILE_APPEND);
}
//This is executing in my plugin page in tools section
function myplugin()
{
//For checking permissions
file_put_contents('data.txt', date("Y-m-d H:i:s") . "Task beginrn", FILE_APPEND);
wp_schedule_event( time(), 'tenmins', 'my_task_hook' ); // hourly, daily and twicedaily
echo "SCHEDULE ACTION";
...
}
For checking that i have created really the cron job, i using for example this plugin http://wordpress.org/extend/plugins/cron-view/. This plugin says me this “Entry #10: my_task_hook â action exists”.
But nothing is happening, the file is not written, what is the problem?
Edit:
I have added one line in myplugin function for see if i have permission for writing files. In fact, i have got permissions, a data.txt file is created in wp-admin/ folder.
Edit2:
I just to understand the cron jobs in wordpress!
Cron in wordpress is not a real cron, it only fires when any user opens the webpage, if no one opens the page, the process won’t fire. So, if a blog has not visitors, cron jobs dont work.
Please, correct me if i am in a mistake.
It could be because you don’t have write permissions to write to the folder where data.txt is in. As you haven’t explicitly defined which folder to write to, it will write to the folder your PHP file is in.
1) right. check is your directory writable…
2) where is your cron scheldule activated?
3) almost imposible situation which i got on work – Check do you have working cron… simple by adding mail or something like that to your hook_action code. on some servers due dns problems (many networked servers and url routes issue) server name not responding while http://yourservername.com requested (by wp-cron).