fbpx
Search
Close this search box.

Home / Blog

 / 
How to schedule WordPress Cron to run on a specific date

How to schedule WordPress Cron to run on a specific date

WordPress Cron is pretty basic and doesn’t allow for much configuration in terms of schedules. By default, you can run your events with a specific interval, like daily, weekly, or monthly (or with intervals of any amount of seconds, really). There is a tricky way to run your event on a specific hour, day of the week or month. Let’s see how to do it!

Understanding WordPress Cron Schedules

As mentioned in the beginning, WordPress can run its Cron only with intervals expressed in seconds. By default, WordPress provides the following intervals:

  • hourly
  • twicedaily (every 12 hours)
  • daily
  • weekly

You can define your own, but with a little bit of code:

add_filter('cron_schedules', 'exampleAddCronInterval');
function exampleAddCronInterval($schedules)
{ 
    $schedules['five_seconds'] = [
        'interval' => 5,
        'display' => esc_html__('Every Five Seconds'),
    ];

    return $schedules;
}

You can do the same via simple UI using our free Advanced Cron Manager plugin

How to schedule WordPress Cron to run on a specific date 1
Advanced Cron Manager – Adding Custom Schedule

Having this custom interval you can schedule your event:

wp_schedule_event(
    $firstRunningTime, // Timestamp of the first execution
    'five_seconds', // Custom schedule key
    'your_event_hook', // Your action hook
    [] // List of arguments
);

All you can control is when the event will be executed for the first time. Then you don’t know when exactly the event will run.

Server Scheduler – More Accurate WordPress Cron

By default, WordPress Cron is triggered by your visitors. That means the website with a very low traffic won’t spawn the WordPress Scheduler as often as needed. Having an event with 5 seconds schedule and someone visiting your website every hour, will cause that event to run every hour instead.

To make it more accurate, you need something more reliable to spawn WP Cron. It’s very convenient that all servers have their own schedulers! In Crontab, you can simply define to run a specific script at specific interval, which will mimic a visitor on your website.

If you’re on a shared hosting, you can ask the hosting support to do the below for you. If you’re managing your server on your own, simply login to the root account and run the Crontab in edit mode, like so:

crontab -e

At the bottom of the editor place your schedule, ie:

*/5 * * * * wget -qO- https://example.com/wp-cron.php &> /dev/null

This will ping your WordPress every 5 minutes. You can adjust that time depending on your needs.

The best way to run that task is using WP CLI, if it’s supported by your hosting. For example, if you’d like to spawn cron every minute you can add something like this:

* * * * * cd /wp/directory && wp cron event run --due-now

Please refer to this simple tool to find out how to express your desired schedule.

The last thing to do is disable WordPress Cron spawning on the front-end with a simple constant added to your wp-config.php file. This step is optional but will make your website more optimized as visitors won’t wait for the Cron to load.

define('DISABLE_WP_CRON', false);

That’s it! Now your Cron will be spawned more reliably. But we still cannot run it on a specific day…

WordPress Cron at a Specific Day and Hour Using Code

The recipe is very simple. You schedule the single event and schedule another one from within the action. Let’s see the example of running a task every Friday.

First, define the single event:

wp_schedule_single_event(
	$firstExecutionTimestamp, // When the event runs for the first time 
	'custom_cron_every_friday', // Your action
	[]
);

Then, handle that event, and from the callback body, schedule another event at a desired time, day of week, day of month or whenever you want the next occurrence to happen.

add_action('custom_cron_every_friday', function () {
	// Do your thing here...

	// Find the next Friday timestamp
	$nextFriday = strtotime('next friday');

	wp_schedule_single_event(
		$nextFriday,
		'custom_cron_every_friday', // The very same action
		[]
	);
});

You can use various techniques to find the next occurrence timestamp, but strtotime function is the simplest one.

There’s an even simpler way, though.

WordPress Cron at Specific Day and Hour Using UI

Instead of managing those events via the code, you can use very simple UI of the Advanced Cron Manager PRO plugin. What’s so special about it is how you can define your schedules. And that’s with Schyntax, a simpler and more intuitive way of defining cron schedules.

Would you like to run your WordPress Cron job every Friday? Nothing simpler:

dayofweek(fri)

Or maybe you require it to run every working day at 9:30am? Easy-peasy:

hours(9) minutes(30) daysOfWeek(mon..fri)

If you don’t know how to use Schyntax, you don’t even have to look at the docs. Our AI integration will help you define the proper schedule. Simply describe when you’d like to run your events and we’ll generate schyntax for you, automagically!

How to schedule WordPress Cron to run on a specific date 2
Generating Schyntax with AI

With Advanced Cron Manager PRO it’s very simple to schedule your Cron job on a specific hour or day or day of week or month, even without the technical knowledge.

How to schedule WordPress Cron to run on a specific date 3
Managing custom day schedule with ACM PRO

Additionally, you can reschedule 3rd party jobs to run at those schedules! That includes default WordPress events or any other plugin events. As a bonus, you get detailed insight on when exactly the event run and how much time did it take.

How to schedule WordPress Cron to run on a specific date 4

Advanced Cron Manager PRO

$39  / YEAR

Custom Cron Schedules

Run your WordPress Cron at the specific hour or day.

Total Cron Control

Detailes insights and Cron logs. See when exactly the WordPress Cron events run, how much memory and time they took.

Support & Updates

Top-notch support backed by amazing reviews. Future updates of plugin and extensions.

Share This Article:

Picture of Kuba Mikita

Kuba Mikita

Share This Article:

Summary

Picture of Kuba Mikita

Kuba Mikita

Share This Article:

Latest updates, discounts and more!

Get the most recent updates on WordPress automation, fresh plugins, new features and price cuts.

Related articles

As a marketer diving into the technical realm I 8217 ve often highlighted the benefits of implementing a notification strategy on WordPress sites Now it 8217 s time to delve...

In today 8217 s online world your WordPress website isn 8217 t just a spot on the internet It 8217 s the face of what you do whether that 8217...

Are you experiencing challenges with WordPress email notifications not arriving as expected This is a common problem in the WordPress community but don 8217 t worry This guide is specifically...

WordPress Plugins that get the job done

With years of experience creating client websites, we bring our experience to functional WordPress plugins.

Easy to use for everyone

Regardless of experience level, you can easily use our products.

Top-notch Support included

We treat our clients seriously and focus heavily on support. Reviews backs this up!

Extensible and developer friendly

You're the same developer as we are. Our code is clean and extensible, just the way you like.