I’m creating a WordPress plugin that will copy posts data to a remote database, I know it will take a while to transfer all the posts.
How should I create copy functionality so it won’t timeout? Ideally it would run in background and show some notification on completion.
You could use WordPress’ pseudo-cron and wp_schedule_single_event
.
<?php
// add the action.
add_action('wpse71941_cron', 'wpse71941_long_running');
function wpse71941_long_running($args)
{
// might need to call `set_time_limit` here
set_time_limit(0);
// do long running stuff here
// return normal time limit
if($l = ini_get('max_execution_time'))
set_time_limit($l);
}
// schedule the event for right now
wp_schedule_single_event(
time(),
'wpse71941_cron',
array('args' => 'for', 'callback' => 'function')
);
Not sure if you need to mess with time limit. WP does call ignore_user_abort
at the top of the cron script.