创建cron作业WordPress时出现问题

时间:2017-12-13 作者:user9005364

我目前正在尝试创建一个wordpress插件,在那里我可以交易比特币。我应该创建一个cron作业,以便检查从一些API调用中获得的值,但我的cron作业没有正确激活。

我还试着做一些随机的“回显”,看看问题是否出在我的API代码上,但我认为问题在于wordpress没有激活我的cron作业。

这是我的代码:

    function my_activation() {
    if (!wp_next_scheduled ( \'my_periodic_event\' )) {
    wp_schedule_event(time(), \'thirtysec\', \'my_periodic_event\');
    }
    add_action(\'my_periodic_event\', \'checkCurrencyValue\');
}

    function checkCurrencyValue() {
        global $market, $quantita, $durata, $wpdb, $table_name;
        $table_name = $wpdb->prefix."bittrex_account_info";
        $users = $wpdb->get_results( "SELECT chiave FROM $table_name");
                    // The query will get the complete users login id/login name
        foreach ( $users as $user ) {
        echo $user->chiave;
        }
        $users = $wpdb->get_results( "SELECT chiave FROM $table_name WHERE email = \'[email protected]\'");
        $wpdb->update( $table_name, array( \'chiave\' => $key, \'secret\' => $users),array(\'email\'=>\'[email protected]\'));

        $d = new BittrexxApi ($key, $secret);
        $quantita = $wpdb->get_var("SELECT quantita FROM wp_bittrex_account_info WHERE email = \'$mail\'");
        $discesaMax = $wpdb->get_var("SELECT discesaMax FROM wp_bittrex_account_info WHERE email = \'$mail\'");
        $maxValDol = $wpdb->get_var("SELECT maxvaldol FROM wp_bittrex_account_info WHERE email = \'$mail\'");
        $summ = $d->getMarketSummary("USDT-BTC");
        if($summ<discesaMax){
            //$balance = $d->buyLimit($market, $quantita, $durata);
        }
        else if($summ>maxValDol){
            //$balance = $d->sellLimit($market, $quantita, $durata);
        }       
}
我的寄存器挂钩正在工作:执行My\\u activation()函数,但不执行checkCurrencyValue()函数。有什么问题吗?

提前谢谢你,菲利波

1 个回复
最合适的回答,由SO网友:David Sword 整理而成

下面是我用于设置Wordpress cron的缩小参考,它来自wp_schedule_event()cron_schedules:

安装Cron

// SETUP CRON
add_action(\'wp\', \'myplugin_schedule_cron\');
function myplugin_schedule_cron() {
  if ( !wp_next_scheduled( \'myplugin_cron\' ) )
    wp_schedule_event(time(), \'daily\', \'myplugin_cron\');
}
Cron函数
// the CRON hook for firing function
add_action(\'myplugin_cron\', \'myplugin_cron_function\');
#add_action(\'wp_head\', \'myplugin_cron_function\'); //test on page load

// the actual function
function myplugin_cron_function() {
    // see if fires via email notification
    wp_mail(\'[email protected]\',\'Cron Worked\', date(\'r\'));
}
自定义Cron时间间隔如果Wordpress的默认小时、每日时间不够,您可以创建自己的时间间隔。

// CUSTOM TIME INTERVAL
add_filter(\'cron_schedules\', \'myplugin_cron_add_intervals\');
function myplugin_cron_add_intervals( $schedules ) {
  $schedules[\'customTime\'] = array(
    \'interval\' => 30,
    \'display\' => __(\'Every 30sec\')
  );
  return $schedules;
}
更换daily 在里面wp_schedule_event() 具有customTime

查看cron您还可以通过带有插件的gui查看cron:https://wordpress.org/plugins/search/cron+view/

结束