7

I need details about wp-cron. I used wp_schedule_event for automatically sending emails.

But wp_schedule_event is only triggered when a user visits our site. How to make the cron run automatically?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
softsdev
  • 1,478
  • 2
  • 12
  • 27

5 Answers5

13

wp_cron does not run all the time in the background the way the name might suggest. That kind of scheduling isn't possible for a web application like WordPress, since the WordPress script only runs when someone is viewing the site and it does not run when no one is looking. What happens instead is that when the WordPress script boots, the cron values are checked and if one is (over)due it gets executed. It may be a few minutes late or a few hours late, but that is the way it works. The wp_cron jobs run at the first opportunity, basically.

If you want to run a script when someone visits the site, you don't really want wp_cron at all. wp_cron doesn't trigger when someone visits. It is a fuzzy timer. To run when someone visits you are going to have to think it through some. You could put a function in your theme's functions.php but it would run on every page load, not just on the first load of the visit. You could hook to wp_login and run your function when someone logs in. You are going to have to decide what counts as a 'visit' first.

s_ha_dum
  • 2,840
  • 2
  • 18
  • 23
  • I want my script works like Cron job at server using code it this possible? – softsdev Oct 15 '12 at 14:27
  • 1
    That is exactly how I said ***it does not work*** and the nature of the HTTP protocol and of a PHP based application makes it next to impossible. You need something like real *nix cron to do that. I am sorry. That is the answer. I wish it were more what you wanted. – s_ha_dum Oct 15 '12 at 14:44
13

It will run at first visit right after the scheduled time.
1.- yes, run automatically, but triggered by a visitor.
2.- it's enough to sending email in a scheduled basis.


For a real cron behavior you can run a script using crontab

crontab -e
wget -q http://domain.com/wp-content/some-path/your-script.php

-q: quiet (no output). Just run the script. Otherwise you will have a lot of files in your server (wget pull files)

Tip: http://www.thegeekstuff.com/2011/07/php-cron-job/


Using cpanel: http://wiki.hostbillapp.com/index.php?title=Settings:_cPanel:_Cron_job

enter image description here

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • is this possible to trigger automatically at scheduled time? not depends on visitor – softsdev Oct 15 '12 at 14:22
  • Yes, see my updated answer. I have done it many times for scraping jobs! – Igor Parra Oct 15 '12 at 14:28
  • Can you please tell me where should i have to run/write this script? – softsdev Oct 15 '12 at 14:39
  • Wherever you want in your public website. It is equal to any other php file `http://domain.com/wp-content/some-path/your-script.php` – Igor Parra Oct 15 '12 at 14:42
  • I already scheduled event using Cpanel Cron I'm checking is there to directly using Code Actually I create one plugin in wordpress so this Plugin may be use at different site that's why I am searching solution on that – softsdev Oct 15 '12 at 15:07
  • with crontab i had to use this to avoid filling my webserver with a lot of files: wget -q -O - http://yoursite/wp-cron.php?doing_wp_cron >/dev/null 2>&1 – manuel-84 Oct 05 '16 at 14:54
4

It seems that every one has just answered the way a *nix cron should be setup. Where as the real answer should be wordpress specific. There is a setting that can be done in the wp-config.php

define('DISABLE_WP_CRON', 'true');

Now if you can configure the *nix cron like Igor Parra answered, the background and offset cron jobs will not run any more, where as the time set in the control panel or crontab will be in effect.

1

You need to set the cron as wel after writing functionality in file..

Refer : http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/

sandip patil
  • 638
  • 4
  • 8
  • Thanks for response but the WP-Cron function runs every time someone visits the WordPress powered website. I want WP-Cron run at defined time not whenever user visit sites because my functionality likes send Event payment notification to attendees whose not paid full payment for paying request – softsdev Oct 15 '12 at 14:19
0

The real thing is always preferable, but it can be done in Wordpress (pure PHP):

http://pippinsplugins.com/improved-cron-plugin-review/