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,...
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 ...
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 ...
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 ...
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,...
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 --...
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 ...
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' ); // ...
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 ...
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 ...
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 ...
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....
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 ...
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 ...
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
...
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');
...
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 ...
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', '...
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 ...
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
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 ...
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....
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
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 ...
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' );
}
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
wp-cron × 443cron × 148
plugin-development × 61
plugins × 47
php × 47
functions × 22
scheduled-posts × 20
custom-post-types × 17
hooks × 14
actions × 13
events × 11
wp-query × 9
custom-field × 9
multisite × 9
wp-mail × 9
email × 8
database × 7
woocommerce-offtopic × 6
customization × 5
loop × 5
ajax × 5
wp-insert-post × 5
date-time × 5
posts × 4
custom-taxonomy × 4