Hot answers tagged

37

You can create new schedule times via cron_schedules: function my_cron_schedules($schedules){ if(!isset($schedules["5min"])){ $schedules["5min"] = array( 'interval' => 5*60, 'display' => __('Once every 5 minutes')); } if(!isset($schedules["30min"])){ $schedules["30min"] = array( 'interval'...


22

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 var_dump(). For ease of use place the following code in the plugin: echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>'; For more info: https://...


18

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 continuously, which can be an issue if there are critical tasks that must run on time. There is an easy solution for this. Simply set up your system’s task ...


16

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 events, when it's set to run, and how often it's rescheduled. Here is what was returned to me when I ran the command: https://kinsta.com/knowledgebase/...


15

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,path,archived,deleted --format=csv --path="$WP_PATH" | grep ",0,0$" | awk -F ',' '{print $1 $2}') do for EVENT_HOOK in $(wp cron event list --format=csv --...


15

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 --path flag when calling cron run. wp cron event run --due-now [<hook>…] One or more hooks to run. [--due-now] Run all hooks due right now. [-...


14

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 explicitly include them by adding include_once( ABSPATH . 'wp-admin/includes/image.php' ); into your call_import function.


12

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' ); // works out to 2016-04-12T00:00:00+00:00 Note that these are PHP functions and they ignore WP timezone, since it resets PHP time zone to UTC. So if using these ...


11

Short answer - Nope. Any page request initializes the scheduled queue. It's just an initialize request. Wp-cron request is a standalone request. so requesting URL /somepage you just initialize request to /wp-cron.php However - If cron event doesn't work really well (it's has 1000 db queries e.g. or its requesting a some really long-to-respond resource), ...


11

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 Transients API to implement semaphores... Here's the code: if ( ! wp_next_scheduled( 'my_task_hook' ) ) { wp_schedule_event( time(), 'hourly', 'my_task_hook' ); }...


9

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 timeout - see https://wordpress.org/support/topic/save-a-full-second-on-cron-execution/. Secondly, the spawned job will now be competing with the page load for ...


7

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.php. So you do not need to call the function manually like you did in your my_custom_fn() function. And if you use a plugin like WP Crontrol, you can easily view ...


6

Why not just to do_action($hook) of your cronned event?


6

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 requests with a function that simply * captures the arguments and schedules a cron event to send the email. */ if ( ! defined( 'DOING_CRON' ) || ( defined( '...


6

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'); function myplugin_schedule_cron() { if ( !wp_next_scheduled( 'myplugin_cron' ) ) wp_schedule_event(time(), 'daily', 'myplugin_cron'); } Cron Function // the ...


5

onetrickpony's answer is mostly correct. If you let WordPress do its default thing, or you manually trigger wp-cron by fetching its URL with 'doing_wp_cron' set to a unique value in the GET string, then you can reliably expect jobs to not be executed twice. But if you execute wp-cron.php from the PHP CLI in your crontab, the story is different. Now it will ...


5

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', 'my_unique_plugin_event_hook');


5

This is what normal query run by wp_get_associated_nav_menu_items() looks like: SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.id = wp_postmeta.post_id ) WHERE 1 = 1 AND wp_posts.post_type = 'nav_menu_item' AND (( wp_posts.post_status <> 'trash' AND wp_posts.post_status &...


5

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 business hours, 9:00 AM to 5:30 PM $updates_suspended = (date('Hi') < 0900 || date('Hi') > 1730); define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended ); ...


5

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

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 +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp You can run your scheduled tasks like so $ wp cron event run --due-now --path=/var/www/mywebsite.com/ ...


5

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

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 add_action(): add_action( 'my_hourly_event', 'do_this_hourly' ); This will cause do_this_hourly() to run whenever the scheduled event runs. This works because ...


4

The Problem: When wp-cron.php is called, it includes only: require_once( dirname( __FILE__ ) . '/wp-load.php' ); so the problem you are facing is that wpmu_delete_blog() is undefined when you call it from your remove_blogs_daily() function. Possible Solution: You therefore need to add this line: require_once( ABSPATH . 'wp-admin/includes/admin.php' );...


4

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 other answers.


4

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 this: $schedules = wp_get_schedules(); foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) break; foreach ( (array) $...


4

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 around line 37, and add it. Then setup the system cron to fire up the wp-cron tasks: */5 * * * * wget -q -O - "http://example.com/wp-cron.php?t=`date +\%s`" > ...


4

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 comply with a long list of mass email best practices. (See also: The FTC's CAN-SPAM Act - Compliance Guide.) I recommend that you use (or study the code of) a ...


4

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 And you can add the following line to your wp-config.php to disable crons being run from HTTP requests: define('DISABLE_WP_CRON', true);


4

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 triggered when the site receives a visit. In the scenario * where a site may not receive enough visits to execute scheduled tasks * in a timely manner, this file ...


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