I’m currently trying to create a wordpress plugin where i can trade bitcoins.
I should create a cron job where I want to check a value I get from some API calls, but my cron job isn’t activating properly.

I also tried to do some random “echo” to see if the problem is my API code but I think the problem is that wordpress isn’t activating my cron job.

This is my code:

    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="filippomomente@gmail.com"");
        $wpdb->update( $table_name, array( 'chiave' => $key, 'secret' => $users),array('email'=>'filippomomente@gmail.com'));

        $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 register hook is working: the my_activation() function is executed but not the checkCurrencyValue() function.
Is there some problem?

Thank you in advance, Filippo

1 Answer
1

Here’s the minified reference I use for setting up WordPress cron, which is all from wp_schedule_event() and cron_schedules:

Setup 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 Function

// 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('user@domain.com','Cron Worked', date('r'));
}

Custom Cron Time Interval

if WordPress’s default hourly, daily, aren’t enough, you can create your own.

// 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;
}

replacing daily in wp_schedule_event() with customTime

Viewing Crons

You can also view your cron via a gui with a plugin: https://wordpress.org/plugins/search/cron+view/

Leave a Reply

Your email address will not be published. Required fields are marked *