[wp-trac] [WordPress Trac] #59173: Improve optimizations of get_site_url by not using switch_to_blog

WordPress Trac noreply at wordpress.org
Wed Aug 23 14:15:11 UTC 2023


#59173: Improve optimizations of get_site_url by not using switch_to_blog
------------------------------------+-----------------------------
 Reporter:  PerS                    |      Owner:  (none)
     Type:  defect (bug)            |     Status:  new
 Priority:  normal                  |  Milestone:  Awaiting Review
Component:  Networks and Sites      |    Version:
 Severity:  normal                  |   Keywords:
  Focuses:  multisite, performance  |
------------------------------------+-----------------------------
 I belive a better approach would be to use the `WP_Site_Query` query, as
 in:


 {{{#!php
 <?php
 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
         if ( empty( $blog_id ) || ! is_multisite() ) {
                 $url = get_option( 'siteurl' );
         } else {

                 // Use WP_Site_Query to get the site URL.
                 $site = get_site( $blog_id );

                 $url = $site->domain . $site->path;

                 // If the network has a custom domain, use that instead.
                 if ( $site->domain !== \get_network()->domain ) {
                         $url = get_network()->domain . $site->path;
                 }
         }

         $url = set_url_scheme( $url, $scheme );

         if ( $path && is_string( $path ) ) {
                 $url .= '/' . ltrim( $path, '/' );
         }

         /**
          * Filters the site URL.
          *
          * @since 2.7.0
          *
          * @param string      $url     The complete site URL including
 scheme and path.
          * @param string      $path    Path relative to the site URL.
 Blank string if no path is specified.
          * @param string|null $scheme  Scheme to give the site URL
 context. Accepts 'http', 'https', 'login',
          *                             'login_post', 'admin', 'relative'
 or null.
          * @param int|null    $blog_id Site ID, or null for the current
 site.
          */
         return apply_filters( 'site_url', $url, $path, $scheme, $blog_id
 );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59173>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list