I have a function to be run every 5 minutes. I've referred following from the codex:
<?php wp_schedule_event(time(), 'hourly', 'my_schedule_hook', $args); ?>
I want to run this function just every 5 minuets regardless of when to start. How can I this?
Also it says codex says cron will be run when a visitor visits to the site. Is there any way to run the cron just as per minutes and not waiting for a visit?
let's say the following function should be run every 5 minutes then how can I do that using wp_schedule_event()
or wp_cron
?
function run_evry_five_minutes(){
// codes go here
}
I have a function to be run every 5 minutes. I've referred following from the codex:
<?php wp_schedule_event(time(), 'hourly', 'my_schedule_hook', $args); ?>
I want to run this function just every 5 minuets regardless of when to start. How can I this?
Also it says codex says cron will be run when a visitor visits to the site. Is there any way to run the cron just as per minutes and not waiting for a visit?
let's say the following function should be run every 5 minutes then how can I do that using wp_schedule_event()
or wp_cron
?
function run_evry_five_minutes(){
// codes go here
}
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' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
Now you can schedule your function:
wp_schedule_event(time(), '5min', 'my_schedule_hook', $args);
To only schedule it once, wrap it in a function and check before running it:
$args = array(false);
function schedule_my_cron(){
wp_schedule_event(time(), '5min', 'my_schedule_hook', $args);
}
if(!wp_next_scheduled('my_schedule_hook',$args)){
add_action('init', 'schedule_my_cron');
}
Note the $args parameter! Not specifying the $args parameter in wp_next_scheduled, but having $args for wp_schedule_event, will cause an almost infinite number of the same event to be scheduled (instead of just one).
Finally, create the actual function that you would like to run:
function my_schedule_hook(){
// codes go here
}
I think it is important to mention that wp-cron is checking the schedule and running due scheduled jobs each time a page is loaded.
So, if you have a low traffic website that only has 1 visitor an hour, wp-cron will only run when that visitor browses your site (once an hour). If your have a high traffic site with visitors requesting a page every second, wp-cron will be triggered every second causing extra load on the server.
The solution is to deactivate wp-cron and trigger it via a real cron job in the time interval of you fastest repeating scheduled wp-cron job (5 min in your case).
Lucas Rolff explains the problem and gives the solution in detail.
As an alternative, you could use a free 3rd party service like UptimeRobot to query your site (and trigger wp-cron) every 5 minutes, if you do not want to deactivate wp-cron and trigger it via a real cron job.
I'm afraid that other than waiting for someone to visit your site which runs a function, the only other option is to set up a cron job on your server using something like this https://stackoverflow/questions/878600/how-to-create-cronjob-using-bash or if you have a cpanel style interface on your server, sometimes there is a gui for setting this up.
@johano's answer correctly explains how to set up a custom interval for WP cron job. The second question isn't answered though, which is how to run a cron every minute:
In the file wp-config.php
, add the following code:
define('DISABLE_WP_CRON', true);
Add a cron job (crontab -e
on unix/linux):
1 * * * * wget -q -O - http://example/wp-cron.php?doing_wp_cron
The first part (step 1) will disable WordPress internal cron job. The second part (step 2) will manually run WordPress cron job every minute.
With @Johano's answer (how to run a task every 5 minutes) and mine (how to manually run the cron), you should be able to achieve your goal.
If your site does get heavy traffic then you could try using set_transient()
to run it (very approximately) every 5 minutes, eg:
function run_every_five_minutes() {
// Could probably do with some logic here to stop it running if just after running.
// codes go here
}
if ( ! get_transient( 'every_5_minutes' ) ) {
set_transient( 'every_5_minutes', true, 5 * MINUTE_IN_SECONDS );
run_every_five_minutes();
// It's better use a hook to call a function in the plugin/theme
//add_action( 'init', 'run_every_five_minutes' );
}
You can trigger it in plugin activation instead of on each plugin call:
//Add a utility function to handle logs more nicely.
if ( ! function_exists('write_log')) {
function write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
/**
* Do not let plugin be accessed directly
**/
if ( ! defined( 'ABSPATH' ) ) {
write_log( "Plugin should not be accessed directly!" );
exit; // Exit if accessed directly
}
/**
* -----------------------------------------------------------------------------------------------------------
* Do not forget to trigger a system call to wp-cron page at least each 30mn.
* Otherwise we cannot be sure that trigger will be called.
* -----------------------------------------------------------------------------------------------------------
* Linux command:
* crontab -e
* 30 * * * * wget http://<url>/wp-cron.php
*/
/**
* Add a custom schedule to wp.
* @param $schedules array The existing schedules
*
* @return mixed The existing + new schedules.
*/
function woocsp_schedules( $schedules ) {
write_log("Creating custom schedule.");
if ( ! isset( $schedules["10s"] ) ) {
$schedules["10s"] = array(
'interval' => 10,
'display' => __( 'Once every 10 seconds' )
);
}
write_log("Custom schedule created.");
return $schedules;
}
//Add cron schedules filter with upper defined schedule.
add_filter( 'cron_schedules', 'woocsp_schedules' );
//Custom function to be called on schedule triggered.
function scheduleTriggered() {
write_log( "Scheduler triggered!" );
}
add_action( 'woocsp_cron_delivery', 'scheduleTriggered' );
// Register an activation hook to perform operation only on plugin activation
register_activation_hook(__FILE__, 'woocsp_activation');
function woocsp_activation() {
write_log("Plugin activating.");
//Trigger our method on our custom schedule event.
if ( ! wp_get_schedule( 'woocsp_cron_delivery' ) ) {
wp_schedule_event( time(), '10s', 'woocsp_cron_delivery' );
}
write_log("Plugin activated.");
}
// Deactivate scheduled events on plugin deactivation.
register_deactivation_hook(__FILE__, 'woocsp_deactivation');
function woocsp_deactivation() {
write_log("Plugin deactivating.");
//Remove our scheduled hook.
wp_clear_scheduled_hook('woocsp_cron_delivery');
write_log("Plugin deactivated.");
}
The Cronjob Scheduler plugin allows you to run frequent tasks reliably and timely without anyone having to visit your site, all you need is at least 1 action and a Unix Crontab schedule.
It's very easy to use, and very flexible. You create your own function, and define an action within it. Then you can choose your action from the plugin menu and fire it whenever you want.
I have a possible solution using a schedule function and a recursive WP Ajax function.
file_get_contents()
If counter equal or higher than 60 it will reset counter and await for the next cron job.
If counter multiple of 5 (so at each 5 minutes) it will execute your desired function
And, besides the conditions, it will sleep for 59 seconds sleep(59);
(assuming your function it's a quick one). After the sleep, it will trigger itself using file_get_contents()
again.
Important things to note:
set_time_limit(90);
to try prevent server to break your function before the sleepIt's a solution, not a good one, and it may get blocked by the server. Using an external cron you can set a simple function and the server will use resources on it once at each 5 minutes. Using this solution, the server will be using resources on it all the time.
functions.php
– foolishcoder7721 Commented Nov 10, 2015 at 9:01