Schedule


Overview

As you probably know, WordPress has a built-in scheduling system. It is called also Cron Jobs. WP Bones provides a simple way to add your own schedules.

Create a Schedule Service Provider

You may create your own schedule service provider by following the steps below:

php bones make:schedule MySchedule

In the plugins/Provider directory, the new provider will be created by default. Of course, you may create your schedule service provider manually and in any directory you prefer. You have to change the namespace accordingly.

Edit the Schedule Service Provider bootstrap

/plugin/Providers/MySchedule.php
<?php
namespace WPKirk\Providers;
 
if (!defined('ABSPATH')) {
  exit();
}
 
use WPKirk\WPBones\Foundation\WordPressScheduleServiceProvider as ServiceProvider;
 
class MySchedule extends ServiceProvider
{
 
  // Hook name - used in the WordPress schedule event
  protected $hook = 'schedule_example_event';
 
  // Recurrence - used in the WordPress schedule event
  protected $recurrence = 'twicedaily';
 
  public function boot()
  {
    // You may override this method to set the properties
    // $this->hook = 'schedule_example_event';
    // $this->recurrence = 'twicedaily';
  }
 
  /**
   * Run the scheduled event.
   *
   */
  public function run()
  {
    wpbones_logger()->info('Schedule example event triggered');
  }
}

The run method is the method that will be executed when the schedule event is triggered. You should define a unique hook name and recurrence for your schedule event. You may use the protected properties $hook and $recurrence to set the hook name and recurrence. Of course, you may override the boot method to set these properties. For example, you may set the recurrence from the database or from a configuration file.

/plugin/Providers/MySchedule.php
<?php
namespace WPKirk\Providers;
 
if (!defined('ABSPATH')) {
  exit();
}
 
use WPKirk\WPBones\Foundation\WordPressScheduleServiceProvider as ServiceProvider;
 
class MySchedule extends ServiceProvider
{
 
  // Hook name - used in the WordPress schedule event
  protected $hook = 'schedule_example_event';
 
  public function boot()
  {
    // You may override this method to set the properties
    // $this->hook = 'schedule_example_event';
    
    $this->recurrence = $this->plugin->config('schedule.recurrence', 'twicedaily');
  }
 
  /**
   * Run the scheduled event.
   *
   */
  public function run()
  {
    wpbones_logger()->info('Schedule example event triggered');
  }
}

Load the Schedule Service Provider

Add this new Service Provider to the list of providers in the /config/plugin.php file:

config/plugin.php
  /*
  |--------------------------------------------------------------------------
  | Providers
  |--------------------------------------------------------------------------
  |
  | Here is where you can register the Service Providers.
  |
  */
 
  "providers" => [
    "WPKirk\Providers\MySchedule",
  ],

Clear the schedule event

You don’t need to clear the schedule event when you deactivate the plugin. WP Bones will do it for you. Anyway, you may use the clear method to do any cleanup when the plugin is deactivated.

/plugin/Providers/MySchedule.php
<?php
namespace WPKirk\Providers;
 
if (!defined('ABSPATH')) {
  exit();
}
 
use WPKirk\WPBones\Foundation\WordPressScheduleServiceProvider as ServiceProvider;
 
class MySchedule extends ServiceProvider
{
 
  // Hook name - used in the WordPress schedule event
  protected $hook = 'schedule_example_event';
 
  public function boot()
  {
    // You may override this method to set the properties
    // $this->hook = 'schedule_example_event';
    
    $this->recurrence = $this->plugin->config('schedule.recurrence', 'twicedaily');
  }
 
  /**
   * Optional method to do any cleanup when the plugin is deactivated.
   *
   */
  public function clear()
  {
    // Do any cleanup here
  }
 
  /**
   * Run the scheduled event.
   *
   */
  public function run()
  {
    wpbones_logger()->info('Schedule example event triggered');
  }
}