Wordpress services, tutorials and news

WordPress cron

Do you need to implement a WordPress cron to execute some tasks automatically at a specific interval? Let’s say you want to send an automatic email at a specific time. For this you can schedule this task in WordPress, but you will also need to set up a cron at the server level, in Cpanel, Plesk or command line.

Even if it’s not obvious, WordPress already uses scheduled tasks for internal maintenance. To understand how things work I think we first need a visual way to know what’s going on under the hood. To view what WordPress crons are already running, the easiest way would be to install the Advanced Cron Manager plugin

With Advanced Cron Manager you can manage WP Cron events:

  • view all registered events
  • search events
  • execute manually any event
  • add new events
  • pause events
  • delete (unschedule) events
  • ready-to-copy-paste PHP implementation for each event
  • bulk actions on events
  • block WP Cron spawning and get instructions how to set Server Scheduler

and schedules:

  • view all registered schedules
  • add new schedules
  • edit schedules
  • remove schedules

Let’s see how the Advanced Cron Manager interface looks like:

On the right side you can see the “Schedules” which show the intervals when things (events) are scheduled to happen. On the left side, you can see the events that are created, what schedule are they on, and when they are next due. So in order to have something happening in WordPress at a specific time you need to have a schedule, an event and a real cron on the server. The reason why you need a real cron on the server is that these events happen only when the site is visited, so if you want something to happen at 1 AM, the event will be postponed until someone visits the site. The way to prevent things from not happening because the site is not visited is creating a server cron in Cpanel, Plesk or command line.

How to create a Cron event in WordPress

You can create a new cron in WordPress with wp_schedule_event. To do this, we create a function and we add an action that runs the function and sets the schedule. As the function can actually be run multiple times depending on the WordPress add_action that you choose, we first check id the schedule is already there using wp_next_scheduled.

In the example we add_action the function my_activation, we check with wp_next_scheduled if my_daily_events is already scheduled, and if not, we wp_schedule_event the event called my_daily_events at 01:40:00 and the frequency is daily.

Now we have a schedule and we need to tell what event to run. So we simply make a function, let’s say do_this_daily(), put the code we need to execute inside (we’ve put an wp_mail example) and add_action with the same event name my_daily_events we used in wp_schedule_event.

Using a real server Cron to actually execute the event

Now, as said previously, the WordPress cron is not a real cron. You set an event, let’s say at 01:40, and the next time the site is accessed by someone after 01:40, the event is run. This means that if you don’t have a lot of visitors, the event can be delayed for hours! To prevent this, we need to set a server cron to “visit the site” after 01:40. This usually can be done from cpanel, plesk or the command line.

In Plesk you need to search on the top search “Scheduled tasks”, then click “Add task” and setup a fetch Url task to be executed on your domain at a time after your WordPress cron:

In Cpanel, you need to look for Cronjobs, and fetch the url with the command wget -q -O/dev/null http://www.yourdomain.com , something like this:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wordpress templates

responsive_wp_468x60