可在 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

 

文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者風間

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