John Davidson

php - Wordpress Cron Job: Ignore Cron job during the site's inactivity period

0 comments
Message:


I did Cron job to fetch data from API every minute
But I am facing a problem which is when the site is inactive, a number of tasks that have not been implemented are saved, and when the site becomes active, all of these tasks are executed.


Is there a way for me to ignore all operations during the period of inactivity and request api once and follow up on the continuation of the Cron job


I am referring to accumulated scheduler actions that then all execute at once, when the next visitor comes


function my_schedules($schedules){

if(!isset($schedules["my1min"])){
$schedules["my1min"] = array(
'interval' => 60,
'display' => __('Once every 1 min'));
}

return $schedules;
}

add_filter('cron_schedules','my_schedules');

wp_schedule_event(strtotime('01:00:00'), 'my1min', 'callback_scheduled_my');


add_action( 'callback_scheduled_my', 'callback_scheduled_my' );

function callback_scheduled_my(){

//my code

}


My Answer:
maintenance

If you want to ignore a WordPress cron job during site maintenance, you can simply disable the cron job temporarily. Here's how you can do it:

1. Open your WordPress dashboard and go to the "Tools" section.

2. Click on "Cron Events" to view all the scheduled cron jobs.

3. Find the cron job that you want to ignore during maintenance and note down its hook name.

4. Open your theme's functions.php file or create a new plugin file.

5. Add the following code to disable the cron job:

php
function disable_cron_job_during_maintenance() {
if (is_maintenance_mode()) {
wp_clear_scheduled_hook('your_cron_job_hook_name');
}
}
add_action('init', 'disable_cron_job_during_maintenance');


Replace 'your_cron_job_hook_name' with the actual hook name of the cron job you want to disable.

6. Save the changes and activate the theme or plugin.

Now, the specified cron job will be ignored during site maintenance. Remember to remove or comment out the code once the maintenance is complete to re-enable the cron job.

Rate this post

3 of 5 based on 5859 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us