导航首页 » 站长干货 » 一个函数就解决 WordPress 多站点开发最大的问题,一定要收藏!
一个函数就解决 WordPress 多站点开发最大的问题,一定要收藏!
先简单说下什么是 WordPress 多站点,WordPress 多站点就是一套程序,通过子域名或者目录的方式可以开启无数个子站点,后期还可以绑定域名,看起来和普通的 WordPress 站点没什么区别。 比如我们做的花生小店,就是通过 WordPress 多站点模式支撑起 2 万多个站点的电商小程序 SaaS 服务。 switch_to_blog 之后 知道了 WordPress 多站点,那么下一步就是在 WordPress 多站点环境下做开发,做过这方面开发的朋友知道多站点开发最大的问题,就是切换站点获取数据。 比如用户登录花生小店的商户助手的小程序,要对自己店铺的某个商品进行编辑,我们就要切换到他的店铺,完成之后再切换回来,我们定义了一个相关更新商家商品的函数,为了演示简洁,这里我们只修改标题: function wpjam_update_shop_product($shop_blog_id, $id, $title){ switch_to_blog($shop_blog_id); // 切换到商家的店铺 wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 restore_current_blog(); // 切换回商户助手的站点 } 看起来还好,如果我们需要把更新的信息返回,最简单的办法直接在 wp_update_post 函数前面加个 return: function wpjam_update_shop_product($shop_blog_id, $id, $title){ switch_to_blog($shop_blog_id); // 切换到商家的店铺 return wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 restore_current_blog(); // 切换回商户助手的站点 } 这样就出问题了,因为 return 之后的代码不会被执行的,那么用个变量记录一下结果,切换回再返回: function wpjam_update_shop_product($shop_blog_id, $id, $title){ switch_to_blog($shop_blog_id); // 切换到商家的店铺 $resu站长SEO导航网lt = wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 restore_current_blog(); // 切换回商户助手的站点 return $result; }   这样可以,但是如果程序里面不这么简单,由多个地方出错了,要返回? function wpjam_update_shop_product($shop_blog_id, $id, $title){ switch_to_blog($shop_blog_id); // 切换到商家的店铺 $result = some_function(); // 执行一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } $result = some_other_function(); // 执行另外一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } $result = wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 restore_current_blog(); // 切换回商户助手的站点 return $result; } 这样又出问题了,因为前面错误的返回造成 restore_current_blog() 没有被执行到,当然有同学会说,在每个错误抛出之前,执行 restore_current_blog(),这样当然可以,但是代码看起来真的太太啰嗦了。 function wpjam_update_shop_product($shop_blog_id, $id, $title){ switch_to_blog($shop_blog_id); // 切换到商家的店铺 $result = some_function(); // 执行一些操作 if(is_wp_error($result)){ restore_current_blog(); // 切换回商户助手的站点 return $result; // 如果发生了错误,要抛出错误 } $result = some_other_function(); // 执行另外一些操作 if(is_wp_error($result)){ restore_current_blog(); // 切换回商户助手的站点 return $result; // 如果发生了错误,要抛出错误 } $result = wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 restore_current_blog(); // 切换回商户助手的站点 return $result; }   我们可以使用 try / catch / finally 来保证 restore_current_blog() 一定会被执行。 function wpjam_update_shop_product($shop_blog_id, $id, $title){ try{ switch_to_blog($shop_blog_id); // 切换到商家的店铺 $result = some_function(); // 执行一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } $result = some_other_function(); // 执行另外一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } return wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 }finally{ restore_current_blog(); // 切换回商户助手的站点 } }   这样甚至前面我们连用于保存更新商品的 $result 变量也不需要,因为 finally 关键字保证了 restore_current_blog() 一定会被执行。 wpjam_call_for_blog 但是如果所有的多站点开发都要 try / finally 处理,以及 switch_to_blog 和 restore_current_blog 的切换站点和恢复原站点的操作,还是挺烦人的,很容易出错,所以我写了一个高阶函数,把这部分操作封装起来,你直接需要简单调用即可即可: function wpjam_call_for_blog($blog_id, $callback, ...$args){ try{ switch_to_blog($blog_id); return call_user_func_array($callback, $args); }finally{ restore_current_blog(); } }   只需要传递要执行代码的站点 ID($blog_id)和执行的回调函数($callback)以及相关的参数($args)即可,那么上面的更新函数,我们改一下: function wpjam_update_product($id, $title){ $result = some_function(); // 执行一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } $result = some_other_function(); // 执行另外一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } return wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 } 首先我们就不需要 $shop_blog_id 参数了,函数名也从 wpjam_update_shop_product 改成了 wpjam_update_product,这个函数很明显是商家的商品更新函数,意思是我们无需为多站点开发新函数了,只需要使用 wpjam_call_for_blog 这个高阶函数来调用它: wpjam_call_for_blog($shop_blog_id, 'wpjam_update_product', $id, $title);   如果没有别的操作直接 WordPress 内核自带更新文章函数就可以: wpjam_call_for_blog($shop_blog_id, 'wp_update_post', ['ID'=>$id, 'post_title'=>$title]);   还支持闭包: wpjam_call_for_blog($shop_blog_id, function($id, $title){ $result = some_function(); // 执行一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } $result = some_other_function(); // 执行另外一些操作 if(is_wp_error($result)){ return $result; // 如果发生了错误,要抛出错误 } return wp_update_post(['ID'=>$id, 'post_title'=>$title]); // 更新商品信息 }, $id, $title); 真正做到一个函数就解决 WordPress 多站点开发最大的问题,并且还整合到了 WPJAM Basic 中,所以如果你进行 WordPress 多站点的二次开发,是不是一定要安装使用起来。