WP Schedule Event – Every Day When First Visitor Comes

wp-cron is often called a pseudo-cron, because it doesn’t run on a strict schedule. If nobody visits the site, it doesn’t run. If you schedule a wp-cron event to run, say, every 12 hours, it will run at most every 12 hours. But, if your site has very little traffic, there could be far more than 12 hours between runs.

If you need an event to happen every 12 hours, or every day at the same time, you’ll need to use something outside of WordPress. Common approaches to that include using the system task scheduler (on Linux, this is usually called “cron”) or a third-party service like EasyCron.

Assuming you’re on a Linux server (by far the most common hosting environment), create a small shell script that looks like this:

#!/bin/bash
curl http://yoursite.name.com/wp-cron.php >/dev/null

This will run the “curl” command, to load your site’s wp-cron.php page, then immediately ignore the results (by dumping the output to /dev/null). You don’t care about any output the page might have, and in fact there normally isn’t any output. You just want to be sure the page is loaded.

Then, add a line to your crontab (“crontab -e” will open the crontab file in your default editor, probably vi) that looks something like this:

0 0,12 * * *             /home/user/touch-site.sh

(Obviously, modify the file location to wherever you put your script. Also, be sure the script is executable by running “chmod 700 touch-site.sh”.)

This example will visit your site daily, at noon and midnight (server time), and run the wp-cron script. For more details on how this file works, run “man crontab”.

Leave a Comment