[wp-trac] [WordPress Trac] #30209: Media upload in multisite while switched to blog sets wrong url to uploaded media.

WordPress Trac noreply at wordpress.org
Fri Oct 31 09:40:29 UTC 2014


#30209: Media upload in multisite while switched to blog sets wrong url to uploaded
media.
--------------------------+-----------------------------
 Reporter:  yo-l1982      |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Media         |    Version:  4.0
 Severity:  normal        |   Keywords:
  Focuses:  multisite     |
--------------------------+-----------------------------
 To reproduce.
 Setup a WP multisite network and run this pseudo code from a blog that is
 NOT blog_id = 1.

 {{{

 $post_id = 1234; //A post id to an existing post in blog 1
 $image_url = 'http://example.com/image.jpg'; // Existing image.
 switch_to_blog(1);
 $url = media_sideload_image($image_url, $post_id);
 restore_current_blog();
 var_dump($url);
 }}}

 The url will now point at the correct filepath but wrong domain, the
 domain in blog 2 is used.

 My current workaround:
 {{{
 function fix_url_bug($upload_info) {
         global $blog_id;
         $correctDomain = parse_url(get_site_url($blog_id));
         $faultyUrl = parse_url($upload_info['url']);

         $scheme   = isset($faultyUrl['scheme']) ? $faultyUrl['scheme'] .
 '://' : '';
         $host     = isset($correctDomain['host']) ? $correctDomain['host']
 : '';
         $port     = isset($faultyUrl['port']) ? ':' . $faultyUrl['port'] :
 '';
         $path     = isset($faultyUrl['path']) ? $faultyUrl['path'] : '';
         $query    = isset($faultyUrl['query']) ? '?' . $faultyUrl['query']
 : '';
         $fragment = isset($faultyUrl['fragment']) ? '#' .
 $faultyUrl['fragment'] : '';

         $upload_info['url'] = "$scheme$host$port$path$query$fragment";
         return $upload_info;
 }


 $post_id = 1234; //A post id to an existing post in blog 1
 $image_url = 'http://example.com/image.jpg'; // Existing image.
 switch_to_blog(1);
 add_filter('wp_handle_upload', 'fix_url_bug');
 $url = media_sideload_image($image_url, $post_id);
 remove_filter('wp_handle_upload', 'fix_url_bug');
 restore_current_blog();

 var_dump($url);
 }}}



 If needed:
 Watch out for the "reject_unsafe_urls" setting when uploading file from
 locally when reproducing.
 Workaround for that:
 {{{
 add_filter('http_request_args', 'allow_unsafe_url');
 funtion allow_unsafe_url($args)
 {
       $args['reject_unsafe_urls'] = false;
       return $args;
 }
 }}}

 Bye bye.

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


More information about the wp-trac mailing list