2013-07-08 16 views
6

Quando pianifico un evento nella parte superiore del file del plugin principale (plugin.php), cron viene aggiunto all'opzione wp_options cron.wp_schedule_event() non funziona all'interno della funzione di attivazione della classe

wp_schedule_event(time() + 10, 'hourly', 'this_is_my_action');

Questo funziona bene, si aggiunge il nuovo cron. Ma, quando provo ad utilizzare la stessa funzione nella mia funzione di attivazione all'interno della classe plugin, non funziona.

All'interno plugin.php ho:

$plugin = new My_Plugin(__FILE__); 
$plugin->initialize(); 

All'interno My_Plugin classe che ho:

class My_Plugin{ 

    function __construct($plugin_file){ 
     $this->plugin_file = $plugin_file; 
    } 

    function initialize(){ 
     register_activation_hook($this->plugin_file, array($this, 'register_activation_hook')); 
    } 

    function register_activation_hook() 
    { 
     $this->log('Scheduling action.'); 
     wp_schedule_event(time() + 10, 'hourly', 'this_is_my_action'); 
    } 

    function log($message){ 
     /*...*/ 
    } 

} 

Il registro viene scritto quando attivo il plugin, ma il cron non viene aggiunto al database di wordpress. Tutte le idee perché?

+0

Queste funzioni sono esattamente la stessa cosa? –

+0

sì, aggiunge il cron nel mio file plugin principale, anche se l'azione non esiste. ma non funziona all'interno della funzione di attivazione – user2103849

risposta

-1

Prova questo:

class My_Plugin{ 

    function __construct($plugin_file){ 
     $this->plugin_file = $plugin_file; 
    } 

    function initialize(){ 
     register_activation_hook($this->plugin_file, array($this, 'register_activation_hook')); 
    } 

    function register_activation_hook() 
    { 
     $this->log('Scheduling action.'); 
     wp_schedule_event(time() + 10, 'hourly', array($this,'this_is_my_action')); 
    } 

    function this_is_my_action(){ 
    //do 
    } 

    function log($message){ 
    } 

} 

è necessario aggiungere array($this,'name_function') alla pianificazione.

+1

Fare 'array ($ this, 'this_is_my_action')' in 'wp_schedule_event' non funziona. – kel

2

È necessario definire voi l'azione è stato registrato con il vostro evento in programma:

class My_Plugin{ 

function __construct($plugin_file){ 
    $this->plugin_file = $plugin_file; 
} 

function initialize(){ 
    register_activation_hook($this->plugin_file, array($this, 'register_activation_hook'));   
    add_action('this_is_my_action', array($this, 'do_it'); 
} 

function register_activation_hook() 
{ 
    if (!wp_next_scheduled('this_is_my_action')) { 
     $this->log('Scheduling action.'); 
     wp_schedule_event(time() + 10, 'hourly', 'this_is_my_action'); 
    } 
} 

function this_is_my_action(){ 
//do 
} 

function log($message){ 
} 

function do_it() { 
    // This is your scheduled event 
} 

} 
Problemi correlati