I want to add to wp_schedule_event when the plugin is activated, the code I need does run on the hour, but seems to run twice and I can’t work out why, I expect I am doing something incorrect with the way I am calling this.

This is what I have

plugin.php file

namespace GrantName {
  if ( ! defined('ABSPATH' ) ){
    die();
  }
  class Plugin {
    private static $instance = null;
    private static $plugin_dir;  

    private function __construct(){
      $autoloader_path = self::getPluginFilePath("/autoloader.php");
      require_once($autoloader_path);
      Autoloader::register();

      new AdminSupport();
      $adminClass = new AdminSupport(); 
      **register_activation_hook(__FILE__, array($adminClass, 'my_activation'));
      register_deactivation_hook( __FILE__, array($adminClass,'my_deactivation' ));**

    }

AdminSupport.php

namespace GrantName;
class AdminSupport {

  public function onInitialize(){
    add_action('my_hourly_event', array($this, 'do_this_hourly' ));
}

  public function my_activation() {
    wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
  }

  public function do_this_hourly() {
    // do something every hour
    $this->assessplugins();
  }

  public function my_deactivation() {
    wp_clear_scheduled_hook( 'my_hourly_event' );
  }

public function assessplugins(){
//Does some things
}

};

It seem that the scheduled tasks runs twice, one after the other. I installed a plugin to show the tasks and I see that the job has two actions (do_this_hourly() twice).

Can anyone see what I am doing wrong

Thanks

Grant

Read more here: wp_schedule_event seems to be being added twice


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: