简体   繁体   中英

Cron and WordPress don't work. What have I forgotten?

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 it\r\n", 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 begin\r\n", 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.

可能是因为您没有写权限来写入data.txt所在的文件夹。由于您没有明确定义要写入的文件夹,因此它将写入您的PHP文件所在的文件夹。

1) right. check is your directory writable...

if (is_writable(dirname(__FILE__))){
    file_put_contents(dirname(__FILE__).'/data.txt', date("Y-m-d H:i:s") . "task do it\r\n", FILE_APPEND);

} else {
    mail('yourmail@example.com', 'oops!', 'error writing');
}

2) where is your cron scheldule activated?

add_action('activate_' . __FILE__, 'plugin_activate_demo'));    
add_action('deactivate_' .__FILE__,  'plugin_deactivate_demo'));
function plugin_activate_demo(){
    wp_schedule_event( time(), 'tenmins', 'my_task_hook' );  
}

function plugin_deactivate_demo(){
    wp_clear_scheduled_hook('my_task_hook' );  
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM