i´m trying to run a function with a schedule but the response is empty. I am developing a plugin where i will send a json encoded array to my IoT service. The service is fine. All other functions are working fine. i want to send the update counts to my iot service.
What i have is this - my function:
function send_count_updates_to_iobroker() {
$updates = wp_get_update_data();
//Generate text if title is empty
if ( empty($updates['title'] )) {
$updatesTitle = "No updates available";
} else {
$updatesTitle = $updates['title'];
}
$datum_uhrzeit = datum_uhrzeit();
$serviceURL = get_option( 'iob_iot_service_url_count_updates' );
$data = wp_json_encode( (object) array(
'plugins' => $updates['counts']['plugins'],
'themes' => $updates['counts']['themes'],
'wordpress' => $updates['counts']['wordpress'],
'translations' => $updates['counts']['translations'],
'total' => $updates['counts']['total'],
'title' => $updatesTitle,
'timestamp' => $datum_uhrzeit
), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
$response = wp_remote_post( $serviceURL . $data );
return $response;
}
This function will be executed every five minutes with a schedule:
// own schedule five minutes
add_filter( 'cron_schedules', 'five_minute_cron_interval' );
function five_minute_cron_interval( $schedules ) {
$schedules['five_minutes'] = array(
'interval' => 300,
'display' => esc_html__( 'Alle 5 Minuten' ), );
return $schedules;
}
I deactivated the wp-cron in wp-config.php and activate the serverside cron:
define( 'DISABLE_WP_CRON', true);
*/5 * * * * wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
The Cronjob is working - every five minutes it fires the events... But the response is always 0:
Aktualisiert: 05.10.2021 - 14:00:01 Uhr
Title: No updates available
Plugins: 0
Themes: 0
Translations: 0
Core: 0
Total: 0
Also i used for tests the plugin Advanced Cron Manager where i can execute the events manually. This works fine. The response is:
Aktualisiert: 05.10.2021 - 14:01:00 Uhr
Title: 2 Plugin-Aktualisierungen
Plugins: 2
Themes: 0
Translations: 0
Core: 0
Total: 2
Can anyone tell me what i´m doing wrong?
Thanks a lot!
nukleuz
//Edit:
Here is the missing code:
register_activation_hook( __FILE__, 'jb_iobroker_iot_activation' );
add_action( 'jb_iobroker_iot_hourly_event', 'do_this_hourly' );
add_action( 'jb_iobroker_iot_5min_event', 'do_this_five_min_interval' );
function jb_iobroker_iot_activation() {
wp_schedule_event( time(), 'hourly', 'jb_iobroker_iot_hourly_event' );
wp_schedule_event( time(), 'five_minutes', 'jb_iobroker_iot_5min_event' );
}
function do_this_hourly() {
checkPluginUpdates();
send_bloginfo_to_iobroker();
}
function do_this_five_min_interval() {
send_count_updates_to_iobroker();
}
register_deactivation_hook( __FILE__, 'jb_iobroker_iot_deactivation' );
function jb_iobroker_iot_deactivation() {
wp_clear_scheduled_hook( 'jb_iobroker_iot_hourly_event' );
wp_clear_scheduled_hook( 'jb_iobroker_iot_5min_event' );
}
// own schedule five minutes
add_filter( 'cron_schedules', 'five_minute_cron_interval' );
function five_minute_cron_interval( $schedules ) {
$schedules['five_minutes'] = array(
'interval' => 300,
'display' => esc_html__( 'Alle 5 Minuten' ), );
return $schedules;
}
wp-cron.php
or the response from your IOT service? No cron jobs are registered in the code in your question, did you miss out parts?0
? And not PHP trying to marshalundefined
into a falsey value that resolves to0
?wp_get_update_data
is full of checks to test if the current user can do something, but cron jobs don't have a current user. Wouldn't it make more sense to hook into when these update checks happen and send the data on that hook instead?