Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

wp-async-task

> 编程语言
Open source

TechCrunch WP Asynchronous Tasks TechCrunch WP Asynchronous Tasks plugin for TechCrunch.com Quick Start WP Async Task can be installed as a…

728 stars0 likes1 views
WebsiteGitHub

About

TechCrunch WP Asynchronous Tasks TechCrunch WP Asynchronous Tasks plugin for TechCrunch.com Quick Start WP Async Task can be installed as a…

TechCrunch WP Asynchronous Tasks

TechCrunch WP Asynchronous Tasks plugin for TechCrunch.com

Quick Start

WP Async Task can be installed as a plugin or bundled in other plugins or a theme. The class definition is wrapped in a class_exists check, so it will never run the risk of being accidentally defined twice. Just make sure that the plugin file is being included somehow.

Next, you need to extend the class with your own implementation. Implementations of the class act on an arbitrary action (e.g., 'save_post', etc). There are three parts that must be present in any class extending WP_Async_Task:

  1. A protected $action property
  2. A protected prepare_data() method
  3. A protected run_action() method
php
 $post_id );
}

If for any reason the asynchronous task needs to be canceled, you will need to throw an exception:

php
protected function prepare_data($data){
	$post_id = $data[0];
	$post = $data[1];
	if( 'post' !== $post->post_type ) {
		throw new Exception( 'We only want async tasks for posts' );
	}
	return array( 'post_id' => $post_id );
}

The library will handle catching the exception and will prevent the request from running if it catches an Exception.

run_action()

This method is responsible for running whatever action should trigger the functions that need to run inside the asynchronous request. The convention is to use "wp_async_$this->action", but that is up to the implementation.

php
protected function run_action() {
	$post_id = $_POST['post_id'];
	$post = get_post( $post_id );
	if ( $post ) {
		// Assuming $this->action is 'save_post'
		do_action( "wp_async_$this->action", $post->ID, $post );
	}
}

Make sure that you instantiate your asynchronous task once. Do this no earlier than the 'plugins_loaded' action.

Finally, update the action of any tasks that you wish to move to the asynchronous task.

For example, you might change this:

php
add_action( 'save_post', 'really_slow_process', 10, 2 );

to this:

php
add_action( 'wp_async_save_post', 'really_slow_process', 10, 2 );

Contributing

To contribute, please fork the github repository and submit a pull request.

When submitting pull requests, please make sure your changes do not cause any unit tests to fail. To run the unit test suite, make sure you've installed composer and install the test tools by running

bash
composer install

After you've installed the dev tools, run the unit tests by running

bash
vendor/bin/phpunit

Copyright

© TechCrunch 2014

License

This library is licensed under the MIT license. See LICENSE.md for more details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言