You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress插件后台任务开发问询:无页面依赖且不影响加载速度

WordPress后台任务实现方案:无页面依赖、不影响前端加载

Hey there! Great question—WordPress absolutely has solutions for running background tasks that don’t rely on frontend page visits and won’t slow down your site. Perfect for your API sync scenario where you need to handle 100 calls (at 1 TPS) and refresh entries every 40 minutes. Let’s break down the best options for you:

1. WP Cron(WordPress自带定时任务)

WP Cron is the built-in way to schedule recurring tasks, and while it’s triggered by default when someone visits your site, you can pair it with a server-level cron to make it run reliably without frontend traffic.

Here’s a code example tailored to your needs:

// Register the cron event on plugin load
add_action('wp', 'register_my_api_sync_cron');
function register_my_api_sync_cron() {
    if (!wp_next_scheduled('my_api_sync_event')) {
        // Run every 5 minutes to catch expired entries before the 40-minute window ends
        wp_schedule_event(time(), 'every_five_minutes', 'my_api_sync_event');
    }
}

// Add a custom 5-minute cron interval
add_filter('cron_schedules', 'add_custom_cron_schedules');
function add_custom_cron_schedules($schedules) {
    $schedules['every_five_minutes'] = array(
        'interval' => 300, // 300 seconds = 5 minutes
        'display' => __('Every 5 Minutes')
    );
    return $schedules;
}

// The actual sync logic
add_action('my_api_sync_event', 'run_my_api_sync');
function run_my_api_sync() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'your_custom_table'; // Replace with your table name

    // Step 1: Fetch entries older than 40 minutes (2400 seconds)
    $expired_entries = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT id FROM $table_name WHERE UNIX_TIMESTAMP(last_updated) < %d",
            time() - 2400
        )
    );

    if (empty($expired_entries)) return; // No work to do

    // Step 2: Process in batches of 5 (matches API's 5-entry limit)
    $batch_size = 5;
    $total_batches = ceil(count($expired_entries) / $batch_size);

    for ($i = 0; $i < $total_batches; $i++) {
        $current_batch = array_slice($expired_entries, $i * $batch_size, $batch_size);
        $entry_ids = wp_list_pluck($current_batch, 'id');

        // Call your API (replace with your endpoint)
        $api_response = wp_remote_get('https://your-api-url.com/endpoint?ids=' . implode(',', $entry_ids));
        
        if (is_wp_error($api_response)) {
            error_log('API Sync Error: ' . $api_response->get_error_message());
            continue;
        }

        $response_data = json_decode(wp_remote_retrieve_body($api_response), true);
        if (!$response_data) {
            error_log('API Sync Error: Invalid JSON response');
            continue;
        }

        // Step 3: Update the database with fresh data
        foreach ($response_data as $entry) {
            $wpdb->update(
                $table_name,
                array(
                    'your_data_column' => $entry['your_data_field'], // Replace with your fields
                    'last_updated' => current_time('mysql')
                ),
                array('id' => $entry['id']),
                array('%s', '%s'), // Data types
                array('%d')
            );
        }

        // Enforce TPS limit: wait 1 second between API calls
        sleep(1);
    }
}

Make WP Cron Reliable

To avoid relying on frontend visits, set up a server-level cron job (Linux example):

*/5 * * * * wget -q -O - https://your-site.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

This will trigger WP Cron every 5 minutes regardless of site traffic.

2. WP CLI Command(For Server-Side Control)

If you have SSH access to your server, a WP CLI command is even more reliable—it runs entirely in the background with zero frontend impact.

Add this to your plugin:

// Register WP CLI command
if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('sync-api', 'run_api_sync_cli');
}

function run_api_sync_cli($args, $assoc_args) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'your_custom_table';

    // Same logic as above: fetch expired entries, batch process
    $expired_entries = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT id FROM $table_name WHERE UNIX_TIMESTAMP(last_updated) < %d",
            time() - 2400
        )
    );

    if (empty($expired_entries)) {
        WP_CLI::success('No expired entries to sync!');
        return;
    }

    $batch_size = 5;
    $total_batches = ceil(count($expired_entries) / $batch_size);

    for ($i = 0; $i < $total_batches; $i++) {
        $current_batch = array_slice($expired_entries, $i * $batch_size, $batch_size);
        $entry_ids = wp_list_pluck($current_batch, 'id');

        $api_response = wp_remote_get('https://your-api-url.com/endpoint?ids=' . implode(',', $entry_ids));
        
        if (is_wp_error($api_response)) {
            WP_CLI::warning('API Sync Error: ' . $api_response->get_error_message());
            continue;
        }

        $response_data = json_decode(wp_remote_retrieve_body($api_response), true);
        if (!$response_data) {
            WP_CLI::warning('API Sync Error: Invalid response for batch ' . ($i+1));
            continue;
        }

        foreach ($response_data as $entry) {
            $wpdb->update(
                $table_name,
                array(
                    'your_data_column' => $entry['your_data_field'],
                    'last_updated' => current_time('mysql')
                ),
                array('id' => $entry['id']),
                array('%s', '%s'),
                array('%d')
            );
        }

        sleep(1);
    }

    WP_CLI::success('API sync completed successfully! Processed ' . count($expired_entries) . ' entries.');
}

Then schedule it with a server cron job:

*/5 * * * * cd /path/to/your/wordpress/install && wp sync-api >/dev/null 2>&1

3. WP Background Processing(Third-Party Library)

If you want to handle async queuing without building everything from scratch, the WP Background Processing library is a great choice. It manages task queues and runs them in the background, so your frontend stays fast.

  1. Add the library to your plugin (you can download it and include it in your plugin directory).
  2. Use this code to set up your sync process:
// Include the library
require_once plugin_dir_path(__FILE__) . 'lib/wp-background-processing/wp-background-processing.php';

// Create a background process class
class My_API_Sync_Process extends WP_Background_Process {
    protected $action = 'my_api_sync_process';

    // Process a single task (adjust to batch if needed)
    protected function task($item) {
        $entry_id = $item['id'];
        global $wpdb;
        $table_name = $wpdb->prefix . 'your_custom_table';

        $api_response = wp_remote_get("https://your-api-url.com/endpoint?id=$entry_id");
        
        if (is_wp_error($api_response)) {
            error_log('API Sync Task Failed for ID ' . $entry_id . ': ' . $api_response->get_error_message());
            return false; // Don't retry failed tasks
        }

        $response_data = json_decode(wp_remote_retrieve_body($api_response), true);
        if (!$response_data) {
            error_log('API Sync Task Failed for ID ' . $entry_id . ': Invalid response');
            return false;
        }

        $wpdb->update(
            $table_name,
            array(
                'your_data_column' => $response_data['your_data_field'],
                'last_updated' => current_time('mysql')
            ),
            array('id' => $entry_id),
            array('%s', '%s'),
            array('%d')
        );

        sleep(1); // Enforce TPS limit
        return false; // Mark task as complete
    }

    // Run when all tasks are done
    protected function complete() {
        parent::complete();
        error_log('API Sync Queue completed successfully!');
    }
}

// Initialize the process
$my_api_sync_process = new My_API_Sync_Process();

// Schedule queuing tasks via WP Cron
add_action('my_api_sync_event', 'queue_api_sync_tasks');
function queue_api_sync_tasks() {
    global $wpdb, $my_api_sync_process;
    $table_name = $wpdb->prefix . 'your_custom_table';

    $expired_entries = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT id FROM $table_name WHERE UNIX_TIMESTAMP(last_updated) < %d",
            time() - 2400
        )
    );

    foreach ($expired_entries as $entry) {
        $my_api_sync_process->push_to_queue(array('id' => $entry->id));
    }

    $my_api_sync_process->save()->dispatch();
}

Key Tips for Your Scenario

  • TPS Compliance: Always add sleep(1) between API calls to stay within the 1 TPS quota.
  • Batch Efficiency: Group 5 entries per API call to minimize total requests (100 calls for 500 entries).
  • Error Logging: Use error_log() or WP CLI warnings to track failures—critical for debugging.
  • Database Index: Add an index to your last_updated field to speed up queries for expired entries (avoids slowdowns on large tables).
  • Reliability: For WP Cron, always pair it with a server cron to ensure tasks run even if your site gets no traffic.

内容的提问来源于stack exchange,提问作者beginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:25:07