Hot answers tagged

42 votes

How to run a function every 5 minutes?

You can create new schedule times via cron_schedules: function my_cron_schedules($schedules){ if(!isset($schedules["5min"])){ $schedules["5min"] = array( 'interval' => 5*60,...
user avatar
23 votes

Is there a quick way to view the wp-cron schedule

WordPress has an undocumented function, _get_cron_array(), that returns an array of all currently scheduled tasks. We are going to use a crude but effective method to dump out all the tasks using ...
user avatar
18 votes

Is there a quick way to view the wp-cron schedule

You can use the WP-CLI. From the command line, you can run the following command from the directory of your WordPress installation: wp cron event list It will display a table of the scheduled ...
user avatar
18 votes

Should I disable WP_CRON and instead trigger wp-cron.php from server every few mins?

There is no downside for running WP CRON using the server's cron jobs. In fact this is the recommended practice. According to Official WordPress Plugin Development Document: WP-Cron does not run ...
user avatar
  • 7,214
16 votes

Running WP Cron on multisite the right way

I think the best way is to use WP-CLI but you'd need to write a bash script to do this. Here is one that should do it for you: WP_PATH="/path/to/wp" for SITE_URL in = $(wp site list --fields=domain,...
user avatar
15 votes

Running WP Cron on multisite the right way

After you've added the constant in wp-config.php defined('DISABLE_WP_CRON') or define('DISABLE_WP_CRON', true); WP-CLI And assuming you have your config.yml setup correctly, you can ommit the --...
user avatar
  • 9,479
14 votes
Accepted

Uncaught Error: Call to undefined function wp_generate_attachment_metadata() @ wp-cron

Some of what is usually admin side functionality is not included as part of the "main" wordpress bootstrap, files containing uploaded file manipulation functions are one of them and you need to ...
user avatar
  • 22.8k
12 votes
Accepted

wp_schedule_event() on specific time, daily

time() only returns current time, it doesn't accept any inputs. $time = time(); // works out to 2016-04-11T12:11:34+00:00 What you want is midnight tomorrow: $tomorrow = strtotime( 'tomorrow' ); // ...
user avatar
  • 98k
11 votes
Accepted

How to make sure that only one wp_cron() runs at a time?

Yes, it is possible... And to be honest, it's often very important to do this... WP Scheduler sometimes tends to cause problems, when cron tasks are long... So how I solve this problem? I use ...
user avatar
10 votes

Do WordPress cron jobs slow down page loading?

The short answer is actually yes, in most cases. Firstly, on most set-ups, spawning a cron job incurs a 1 second delay on page load, because it is done via a loopback HTTP request with a 1 second ...
user avatar
  • 201
8 votes

Triggering cron by calling wp-cron.php on the command line rather than with wget?

Looking at the file documentation inside wp-cron.php it seems it's absolutely possible to just call $ php wp-cron.php: /** * A pseudo-CRON daemon for scheduling WordPress tasks * * WP Cron is ...
user avatar
  • 2,923
7 votes
Accepted

Do WordPress' cron's clean up expired transients?

Yes, delete_expired_transients is a cron event that runs once per day and the function delete_expired_transients() is automatically called when the cron event runs — see wp-includes/default-filters....
user avatar
  • 33.2k
6 votes
Accepted

How to make WordPress emails async

Since wp_mail is pluggable, we can selectively override it. I run the following code in my general functionality plugin: /** * Email Async. * * We override the wp_mail function for all non-cron ...
user avatar
  • 675
6 votes

Schedule WordPress Auto-Updates to only run during business hours

This one is actually surprisingly simple; add this to your wp-config.php file and all automatic updates will be blocked when outside of the specified hours: // Suspend updates when outside of ...
user avatar
  • 475
6 votes

Triggering cron by calling wp-cron.php on the command line rather than with wget?

Yes, it's possible to trigger cron runs with just $ php /path/to/wordpress/wp-cron.php. Alternatively you can use curl: */10 * * * * curl http://example.com/wp-cron.php > /dev/null 2>&1 ...
user avatar
  • 225
6 votes
Accepted

Problem creating cron job wordpress

Here's the minified reference I use for setting up Wordpress cron, which is all from wp_schedule_event() and cron_schedules: Setup Cron // SETUP CRON add_action('wp', 'myplugin_schedule_cron'); ...
user avatar
  • 3,227
5 votes

Running WP Cron on multisite the right way

Easier one-liner with less bash: wp site list --field=url | xargs -i -n1 wp cron event run --due-now --url="{}" You can either run it manually or put it in a script and call it from cron as in the ...
user avatar
  • 456
5 votes

How to use wp_schedule_event in a class?

Add an action outside your class definition: add_action('my_unique_plugin_event_hook', array($this,'hook')); And then use this in your event: wp_schedule_event(time(), 'daily', '...
user avatar
  • 161
5 votes
Accepted

Sending email to all users

Sending mass emails that actually get delivered is not a simple matter at all. Even if your code sends 1000 messages correctly, many if not most of them will get blocked or labeled as spam unless you ...
user avatar
  • 106
5 votes
Accepted

Why Should We Use wp_clear_scheduled_hook and What it Does?

If you deactivate the plugin, the scheduled event will still try to run ( and fail ) unless you remove that event from the schedule using wp_clear_scheduled_hook
user avatar
  • 4,900
5 votes

Running wp-cron from CLI

Or, you could use WP-CLI which was developed for scenarios like these. After a short installation like this $ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar $ chmod ...
user avatar
  • 5,955
5 votes

How to run multiple Async HTTP requests in Wordpress?

The (built-in) Requests class lets you call multiple requests simultaneously: Requests::request_multiple. <?php $requests = Requests::request_multiple([ [ 'url' => 'https://www....
user avatar
  • 357
5 votes

wp-cron and execution of code in is_admin() included script

I know it's late, but for other people searching: if (is_admin () || wp_doing_cron ()) { your code ... } about wp_doing_cron
user avatar
5 votes
Accepted

WP Cron - Run Cron after evey 15 minutes at a specific time for the next three hours

Setup WordPress Cron Events Let's say you want to start the cron at 6PM and keep running the cron after every 15 minutes till 9PM (for 3 hours). Then all cron stops. Note: for the simplicity of ...
user avatar
  • 7,214
5 votes

Check if event was scheduled - schedule event only once

Have you ever heard of Rubber Ducking? The official documentation has an answer: if ( ! wp_next_scheduled( 'woofio_hourly' ) ) { wp_schedule_event( time(), 'hourly', 'woofio_hourly' ); }
user avatar
5 votes
Accepted

How to execute existing WP Cron programmatically

When you register a scheduled event for WordPress' cron, the 3rd argument is a hook name: wp_schedule_event( time(), 'hourly', 'my_hourly_event' ); And you add a function to run on this hook with ...
user avatar
4 votes

WP Cron Doesn't Execute When Time Elapses

For anyone protecting their (development) site from public access, HTTP Authentication can be the cause of WP Cron not functioning. In case it can help anyone, here is my list of things I did before ...
user avatar
  • 147
4 votes
Accepted

Is there a known vulnerability for wp-cron.php?

In wp-includes/default-filters.php we can find a callback registration: // WP Cron if ( !defined( 'DOING_CRON' ) ) add_action( 'init', 'wp_cron' ); If we go the function wp_cron() now, we see ...
user avatar
  • 105k
4 votes
Accepted

Why cron doesn't work to me?

Do you have define('DISABLE_WP_CRON', true); set in wp-config? You need it to have the system cron fire up the wp-cron tasks. Go to the bottom of the database settings in wp-config.php, typically ...
user avatar
  • 564
4 votes
Accepted

debugging wp_cron jobs with XDebug in Eclipse

I just found the answer to my question over on stackoverflow...and thought I'd copy the answer over here for future reference since WP folks are probably more likely to look here. When WP_Cron fires ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible