2
function cronstarter_activation() {
  if( !wp_next_scheduled( 'mycronjob' ) ) {
   wp_schedule_event( strtotime( '00:02:00' ), 'daily', 'mycronjob' );
  }
}
add_action('wp', 'cronstarter_activation');

function my_repeat_function() {
    //My code the same information to database
}
add_action ('mycronjob', 'my_repeat_function');

Everything works great. My problem is that I have php code in sidebar to recieve the storaged information from the database that saved from cron_job, and the first time that a visitor visits the site in the new day after the 00:02:00 the recieved information is the previous day information.

I think that the first visitor of day, first see the sidebar's result, which is the previous day information, and after activate the cronjob.

Is that true? How can I run the cronjob without needed the first visitor?

Bbabis
  • 127
  • 1
  • 9
  • `wp_cron()` works only when someone visits your site. It has to be 'triggered' somehow. If you want to have automatic cron that will run independently on user visits, you'll need to set up cron job with pure php on from your server. Check [this answer](http://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php) and [this](https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/). – dingo_d Nov 28 '16 at 06:51

1 Answers1

0

Yes WP Cron have big caveat is that it requires someone to visit the site in order to run. For example, if you schedule your job to run at 2AM but no one visits at 2AM, it won’t run at 2AM. It will run when someone visits the site after 2AM.

More info Official : https://codex.wordpress.org/Function_Reference/wp_schedule_event

More info public blog: http://brianshim.com/webtricks/cron-job-wordpress/

For more control on cron job use schedule at system level: Use PHP to create, edit and delete crontab jobs?

Setup/configuration will differ based on your server & hosting type.

Community
  • 1
  • 1
Ajit Bohra
  • 363
  • 3
  • 9