dimanche 11 octobre 2015

wp_schedule_event() in a class not being run (WordPress plugin)

I've created a plugin, which on activation should schedule a cron job. The plugin is written as a class, and is not working as it should. The example code (to test what is being run) is below:

if(!class_exists('ExampleClass'))
{
class ExampleClass
{

    /**
     * Construct the plugin object
     */
    public function __construct()
    {
        add_action( 'import_product_feeds', array( $this, 'import_data_feeds' ) );

    } // END public function __construct


    /**
     * Activate the plugin
     */
    public static function activate()
    {

       wp_mail( 'my-email@gmail.com', 'Plugin activated', 'The plugin has been activated' );
       wp_schedule_event( time(), 'daily', 'import_product_feeds' );


    } // END public static function activate

    /**
     * Deactivate the plugin
     */     
    public static function deactivate()
    {
        wp_clear_scheduled_hook('import_product_feeds');

    } // END public static function deactivate


   public function import_data_feeds(){

       wp_mail( 'my-email@gmail.com', 'Cron run started', 'The cron job has been started.' );


   }

} // END class ExampleClass
} // END if(!class_exists('ExampleClass'))



if(class_exists('ExampleClass'))
{
// Installation and uninstallation hooks
register_activation_hook(__FILE__, array('ExampleClass', 'activate'));
register_deactivation_hook(__FILE__, array('ExampleClass', 'deactivate'));

// instantiate the plugin class
$ExampleClass = new ExampleClass();

}

On activation, I do get the plugin activated email, however the cron run email does not come through so it's not being called correctly. Might be something obviously wrong with the code.. Anyone has any pointers?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire