可在 WordPress 多站点网络的任何站点上执行功能的代码段

如果需要在 WordPress 多站点的子站中的任何子站,执行任何功能,看尝试使用下面的代码段进行开发工作。

/**
 *  Perform any function on any site on the network
 *
 *  @param    integer         $site_id   site on which to perform the function
 *  @param    string|array    $action    function name to perform
 *  @param    array           $args      parameters passed as an indexed array
 *  @return
 */
function do_network_request( $site_id, $action, $args = array() ) {

   switch_to_blog( $site_id );

      if ( ( is_string( $action ) && function_exists( $action ) ) || ( is_array( $action ) && method_exists( ...$action ) ) )
         $result = call_user_func( $action, ...$args );
      else
         $result = NULL;

   restore_current_blog();

   return $result;

} // end do_network_request

 

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者风间

在哪里跌倒,就在哪里趴着。