Synchronize Data every minute with set_transient

To my understanding, *_transient() is basically *_option() wrapped in a cache-timer.

What that means in this context, is that set_transient( '...', true, * MINUTE_IN_SECONDS ); will not run/re-evaluate every minute. Instead it will run/re-evaluate when the page has loaded by a visitor/user, and the data therein is older than 60 seconds. This is not ideal because what it does is puts the processing of the sync method onto the users load time, which isn’t good, and can mess up, like the hiccup your experiencing – it correlates with this:

It seems to happen when I reload the page on the time my sync-process is running

So your visiting the admin after >60 seconds of no other visits to the site. The _transient is thus expired, and fires your my_task_sync_method() on your page load, which must be echo’ing or interrupting your load processes in some way.


To resolve this, first, you should use WordPress Cron, not _transient. Cron jobs run in the background, and adhere to the 60-second sync, without relying on users visiting.

Here’s a base snippet for a cron task:

// add a 60 second cron schedule
add_filter('cron_schedules', function ( $schedules ) {
    $schedules['everyminute'] = array(
        'interval' => 60,
        'display' => __('Every Minute')
    );
    return $schedules;
});

// register your cronjob
add_action('wp', function () {
    if ( !wp_next_scheduled( 'my_task_sync_cronjob' ) )
        wp_schedule_event(time(), 'everyminute', 'my_task_sync_cronjob');
});

// the registered cron hook, that'll fire your function
add_action('my_task_sync_cronjob', 'my_task_sync_method');

Secondly, you should ensure my_task_sync_method() isn’t echo’ing any data. I am assuming it does given your symptoms.


Please note I’ve placed the functions in the hooks anonymously, I did this for easier display here. You may want to make them callbacks instead incase other developers want to tap into your hooks.