[wp-trac] [WordPress Trac] #41445: post_parent can prevent media from embedding correctly

WordPress Trac noreply at wordpress.org
Thu Jan 4 19:10:49 UTC 2024


#41445: post_parent can prevent media from embedding correctly
-------------------------------------------------+-------------------------
 Reporter:  loboyle                              |       Owner:
                                                 |  adamsilverstein
     Type:  defect (bug)                         |      Status:  reopened
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Media                                |     Version:  4.9.4
 Severity:  normal                               |  Resolution:
 Keywords:  dev-feedback needs-unit-tests has-   |     Focuses:  rest-api
  testing-info needs-patch needs-testing         |
-------------------------------------------------+-------------------------

Comment (by ekazda):

 6+ years and still no fix for this. For users that need to overcome this,
 I added this to my functions file:

 {{{
 function modify_rest_response_featured_image($data, $post, $request) {
     // Get the featured media ID
     $featured_media_id = get_post_thumbnail_id($post->ID);

     if ($featured_media_id) {
         // Get the featured media post
         $featured_media = get_post($featured_media_id);

         // Prepare the attachment response manually
         $media_response = array(
             'id' => $featured_media->ID,
             'date' => $featured_media->post_date,
             'slug' => $featured_media->post_name,
             'type' => $featured_media->post_type,
             'link' => get_permalink($featured_media->ID),
             'title' => array(
                 'rendered' => $featured_media->post_title
             ),
             'author' => $featured_media->post_author,
             // 'acf' => array(),
             'caption' => array(
                 'rendered' => $featured_media->post_excerpt
             ),
             'alt_text' => get_post_meta($featured_media->ID,
 '_wp_attachment_image_alt', true),
             'media_type' => $featured_media->post_mime_type,
             'mime_type' => $featured_media->post_mime_type,
             'media_details' => array(
                 'width' => get_post_meta($featured_media->ID,
 '_wp_attachment_metadata', true)['width'],
                 'height' => get_post_meta($featured_media->ID,
 '_wp_attachment_metadata', true)['height'],
                 'file' => $featured_media->guid,
                 'filesize' =>
 filesize(get_attached_file($featured_media->ID)),
                 'sizes' =>
 add_source_url_to_sizes(wp_get_attachment_metadata($featured_media->ID)['sizes'],
 $featured_media->ID),
                 'image_meta' => get_post_meta($featured_media->ID,
 '_wp_attachment_metadata', true)['image_meta']
             ),
             'source_url' => wp_get_attachment_url($featured_media->ID),
             '_links' => array()
         );

         // Add 'wp:featuredmedia' link to the response
         $data->add_link('wp:featuredmedia', rest_url('wp/v2/media/' .
 $featured_media_id), array('embeddable' => true));

         // Add 'wp:featuredmedia' array to the response, containing the
 media response
         $data->data['wp:featuredmedia'] = array($media_response);
     }

     return $data;
 }

 function add_source_url_to_sizes($sizes, $media_id) {
     foreach ($sizes as $key => &$size) {
         $attachment = wp_get_attachment_image_src($media_id, $key);
         $size['source_url'] = $attachment[0];
     }
     return $sizes;
 }

 function add_custom_rest_prepare_filters() {
     $post_types = get_post_types([], 'objects');

     foreach ($post_types as $post_type) {
         add_filter("rest_prepare_{$post_type->name}",
 'modify_rest_response_featured_image', 10, 3);
     }
 }

 add_action('rest_api_init', 'add_custom_rest_prepare_filters');
 }}}

 I don't love this because I'm sure it adds a decent amount of overhead to
 my response time, but I need this to be reliable and this seemed to solve
 it for me. I just reference my custom data instead of the default data.
 Hope this helps somebody.

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


More information about the wp-trac mailing list