PHP code example of wptechnix / wp-background-jobs
1. Go to this page and download the library: Download wptechnix/wp-background-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/ */
wptechnix / wp-background-jobs example snippets
use WPTechnix\WP_Background_Jobs\Background_Jobs;
function myplugin_jobs(): Background_Jobs {
global $wpdb;
static $jobs = null;
if ( null === $jobs ) {
$jobs = Background_Jobs::create( $wpdb, 'myplugin_tasks' );
}
return $jobs;
}
// Create the tables on activation.
register_activation_hook( __FILE__, static function () {
myplugin_jobs()->install();
} );
// Register hooks, the cron watchdog, and CLI commands on every request.
add_action( 'plugins_loaded', static function () {
myplugin_jobs()->boot();
} );
use WPTechnix\WP_Background_Jobs\Job;
final class Send_Welcome_Email extends Job {
public function __construct( private int $user_id ) {}
public function handle(): void {
$user = get_userdata( $this->user_id );
if ( false !== $user ) {
wp_mail( $user->user_email, 'Welcome', 'Thanks for joining.' );
}
}
}
// Run as soon as possible, in the background.
myplugin_jobs()->dispatch( new Send_Welcome_Email( 42 ) );
// Run in five minutes.
myplugin_jobs()->dispatch( new Send_Welcome_Email( 42 ), delay: 300 );
// Route to a named queue.
myplugin_jobs()->on( 'emails' )->dispatch( new Send_Welcome_Email( 42 ) );
// Queue many jobs in a single insert.
myplugin_jobs()->dispatch_many( [
new Send_Welcome_Email( 42 ),
new Send_Welcome_Email( 43 ),
] );
bash
docker compose run --rm php composer install
docker compose run --rm php bash
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.