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

Read More

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.

Related posts

Leave a Reply

2 comments

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

  2. 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 itrn", 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).