It is recommended to use _ as a prefix. The
Class josh\u task extends wp\async\u task. The{
\/**The
*Action used to trigger this action
*The
*@Var string
*\/The
Protected $action= \
\/**The
*Preparing the session for post data to be sent to the processing task
*The
*@Param array $data attached parameters
*The
*@Return array
*\/The
Protected function prepare_data ($data){
Return array(
‘post\u id’= > $data[0]
);
}The
\/**The
*Perform asynchronous operations
*The
*Call send_to_api()
*\/The
Protected function run\u action(){
If (isset ($\u post[‘post\u id’]) & & 0 maleaction \
}The
}The
}The
Cabling is now ready for classes, so you can integrate all this in a few simple steps. The first step is to instantiate the class in plugins_load or later. Obviously, the WP async task library must exist and contain. It is recommended to install by composer. It can also be installed as a plug-in. This is a very bad idea because you can disable plug-ins. The second thing is to wire the new operation that happens inside the class to the original callback. Its performance is as follows: Add\u action (‘wp\u async\u save\u post’,’josh\u send\u to\u api’);
Function josh_send_to_api ($id){
$thing=get_post_meta ($id, \
$r = wp\u safe\u remote\u post (add\u query\u Arg (‘id’, $thing,’ http:\/\/apiexample.com\/ ‘);
If (! Is\u wp\u error ($r)){
$body = json\u decode (wp\u remote\retain\u body ($r));
If (isset ($body->key)){
Update\u post\u meta ($id,’api\u response’, $body->key);
}Another{
Update\u post\u meta ($id,’api\u response’,’none’);
}The
}The
}Hopefully, more asynchronous articles will help you understand why PHP should be used asynchronously. The actual example now needs to know how to execute using a normal word press site. Php7 supports asynchronous PHP, so even if you use two sessions for asynchronous PHP, it is important to immerse your feet in asynchronous PHP.