4

Having:

pluginname.php

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
    die();
}

define('PLUGINNAME_PLUGIN_URL', plugin_dir_url( __FILE__ ));
define('PLUGINNAME_PLUGIN_DIR', plugin_dir_path(__FILE__));

require_once( plugin_dir_path( __FILE__ ) . 'class-pluginname.php' );

// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( __FILE__, array( 'PluginName', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'PluginName', 'deactivate' ) );

PluginName::get_instance();

class-pluginname.php

    /**
     * Initialize the plugin
     *
     * @since     1.0.0
     */
    private function __construct()
    {
        #add_action('init', array($this, 'widget_add_vars'));

        #add_action('params', array($this, 'catch_widget_query'));
         add_action('dailyExport', array($this, '_generateJson'));

         if (!wp_next_scheduled('dailyExport')) {
             wp_schedule_event( time(), 'daily', 'dailyExport' );
         }
    }

    /**
     * Return an instance of this class.
     *
     * @since     1.0.0
     *
     * @return    object    A single instance of this class.
     */
    public static function get_instance()
    {
        // If the single instance hasn't been set, set it now.
        if ( null == self::$instance )
            self::$instance = new self;

        return self::$instance;
    }

    /**
     * Fired when the plugin is activated.
     *
     * @since    1.0.0
     */
    public static function activate()
    {
        self::_generateJson();
        wp_schedule_event(time(), 'daily', 'dailyExport');
    }

    /**
     * Fired when the plugin is deactivated.
     *
     * @since    1.0.0
     */
    public static function deactivate()
    {
        wp_clear_scheduled_hook('dailyExport');
    }

    public function dailyExport()
    {
        return self::_generateJson();
    }

I was wondering, if you think that is right to define as static the activate and deactivate functions, and if you think that I properly work with the cron event, to schedule daily events.

I wrote my plugin, based on this template https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate

  • You can use the cron job inspector to see if your cron jobs reach WordPress and are set properly. Keep in mind that this doesn't tell if the internals of your cron jobs will run properly. – kaiser Jun 25 '13 at 11:05
  • 1
    wow, that's a great idea and tool... gonna check it out now – w0rldart Jun 25 '13 at 11:06
  • @kaiser Reading the source code, I remembered that I had already ran _get_cron_array() and got pastebin.com/XRkjHhue – w0rldart Jun 25 '13 at 11:27
  • Please don't link to external sources. That's what edits are for. – kaiser Jun 25 '13 at 11:31
7

I was working with wp_cron last week in a plugin and we have a fight and are no longer on speaking terms, but for reference this is what I do;

1) - set the scheduled cron event on register_activation_hook
2) - remove the scheduled cron event on register_deactivation_hook

If you are concerned that your scheduled cron event might get wiped from the database then you can add routine as you already have to check for your cron events next scheduled timestamp;

if (!wp_next_scheduled('dailyExport')) { 
    //schedule event 
    wp_schedule_event( time(), 'daily', 'dailyExport' );
}

My example class;

register_activation_hook(__FILE__, array( 'Killa', 'activated' ) );
register_deactivation_hook( __FILE__, array( 'Killa', 'deactivated' ) );

add_action('plugins_loaded', array ( Killa::get_instance(), 'plugin_setup' ) );

class Killa
{
    protected static $instance = NULL;
    public static function get_instance()
    {
        if ( null === self::$instance )
        {
            self::$instance = new self;
            self::load_files();
        }
        return self::$instance; 
    }

    public function plugin_setup()
    {
        add_action('my_cron_event', array($this, 'my_callback_function'));     
    }

    public function __construct()
    {
        //empty
    } 

    public static function activated() 
    {
        wp_schedule_event( time(), 'hourly', 'my_cron_event');
    }

    public static function deactivated() 
    {
        wp_clear_scheduled_hook( 'my_cron_event' );
    }

    public function my_callback_function()
    {
       //do your thing here... such as generate JSON
    }
}

Bottom line is that it's perfectly OK to define your register activation/deactivation hooks as static functions and more appropriately so when working in a singleton type pattern or when your activated()/deactivated() functions might exist within another class call by your controller.

It's also acceptable to have your registration hooks within the constructor too, depending upon your class structure.

  • Thanks for this. Very helpful. A key point to make is the line: add_action('plugins_loaded', ...) - This will ensure that the cron even will happen after WP has loaded, otherwise a lot of WP functions like wp_mail() will return a fatal error. – Moe May 29 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.