Long running action from plugin

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.

Leave a Comment