[wp-trac] [WordPress Trac] #42562: media: wp_get_attachment_metadata() can return WP_Error object which leads to Fatal error

WordPress Trac noreply at wordpress.org
Wed Nov 15 20:32:00 UTC 2017


#42562: media: wp_get_attachment_metadata() can return WP_Error object which leads
to Fatal error
--------------------------+------------------------------
 Reporter:  indigo74      |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:  4.8.3
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by indigo74):

 I corrected the issue by doing this:
 {{{#!php
 <?php
 function wp_prepare_attachment_for_js( $attachment ) {
         if ( ! $attachment = get_post( $attachment ) )
                 return;

         if ( 'attachment' != $attachment->post_type )
                 return;

         $meta = wp_get_attachment_metadata( $attachment->ID );

         /* ... */

         // Line added below
         if (is_wp_error($meta)) return apply_filters(
 'wp_prepare_attachment_for_js', $response, $attachment, $meta );
         // End of modification

         if ( isset( $meta['filesize'] ) ) {
                 $bytes = $meta['filesize'];
         } elseif ( file_exists( $attached_file ) ) {
                 $bytes = filesize( $attached_file );
         } else {
                 $bytes = '';
         }

         /* ... */
 }
 }}}

 I don't know if this is the proper way to handle this case... I'll let
 someone more versed in WordPress to fix this :)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/42562#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list