Run multiple Wordpress Cron to perform two functions to update the database at various fixed intervals

I completed several cron jobs in Wordpress. First of all, I want to clean, I searched a lot for this problem, but did not find the exact solution. So, I posted here.

The problem is that one cron is running, but the other never starts, and I scheduled an interval every three hours for the first cron, but it sometimes runs several times for a minute to receive several emails. And others are never executed.

Does anyone provide a solution to perform two functions for updating a database at various fixed intervals through Wordpress Cron. Thank you very much in advance.

 //The activation hooks is executed when the plugin is activated
 register_activation_hook(__FILE__, 'activate_one');
 register_activation_hook(__FILE__, 'activate_two');
 //Filter for Adding multiple intervals
 add_filter( 'cron_schedules', 'intervals_schedule' );

 function intervals_schedule($schedules) {
     $schedules['threehour'] = array(
        'interval' => 10800, // Every 3 hours
        'display'  => __( 'Every 3 hours' )
     );
     $schedules['onehour'] = array(
         'interval' => 3600, // Every 1 hour
         'display'  => __( 'Every 1 hour' )
     );

     return $schedules;
  }

  //Schedule a first action if it not already scheduled
  function activate_one() {
      if (!wp_next_scheduled('cron_action_one')) {
          wp_schedule_event( time(), 'threehour', 'cron_action_one');
      }
  }

  //Hook into that action that'll fire threehour
  add_action('cron_action_one', 'execute_one');

  function execute_one()
  {
      //Do something or update in database;
  }

  //Schedule a second action if it not already scheduled
  function activate_two() {
      if (!wp_next_scheduled('cron_action_two')) {
          wp_schedule_event(time(), 'onehour', 'cron_action_two');
      }
  }

  //Hook into that action that'll fire onehour
  add_action('cron_action_two', 'execute_two');

  function execute_two()
  {
      //Do something or update in database;
  }
+4
1

, - cron_action_two, . , , here. Cron , , .

, :

  • :

    register_deactivation_hook( __FILE__, 'my_deactivation');
    function my_deactivation() {
        wp_clear_scheduled_hook('my_hourly_event');
    }
    
  • (YES: wp_clear_scheduled_hook( 'my_hourly_event' );) , (NO: if( ! wp_next_scheduled( 'my_hourly_event' ) ))

!

0

Source: https://habr.com/ru/post/1606743/


All Articles