PHP code example of sebkay / wp-queued-jobs

1. Go to this page and download the library: Download sebkay/wp-queued-jobs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sebkay / wp-queued-jobs example snippets


use WpQueuedJobs\Jobs\Job;

class BackgroundJob extends Job
{
    public function handle()
    {
        // Handle the job
        // Use $this->data to access what was passed with the job when it was added to the queue
    }
}

wpj()
    ->addJob(BackgroundJob::class, 'Data for the background job.')
    ->dispatch();

add_action('template_redirect', function () {
    if (!is_page_template('register-success.php')) {
        return;
    }

    wpj()
        ->addJob(SendWelcomeEmailJob::class, wp_get_current_user())
        ->dispatch();
}, 10, 0);

add_action('import_api_posts', function () {
    wpj()
        ->addJob(ImportApiPostsJob::class, ['offset' => 0, 'max' => 100])
        ->addJob(ImportApiPostsJob::class, ['offset' => 100, 'max' => 100])
        ->dispatch();
}, 10, 0);

if (!\wp_next_scheduled('import_api_posts')) {
    \wp_schedule_event(
        \strtotime("+ 1 days 6am"),
        'daily',
        'import_api_posts'
    );
}