fix: missing remote attachments issue#243
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes missing thumbnails for remote (Algolia-hydrated) attachment results in the search UI by carrying thumbnail metadata on mocked remote WP_Post objects and intercepting WordPress image/thumbnail rendering paths to use that metadata.
Changes:
- Adds remote thumbnail plumbing by storing Algolia
thumbnailrecord data on remoteWP_Postobjects. - Hooks into WordPress image/thumbnail/content rendering filters to output remote thumbnail URLs/HTML for negative-ID (remote) posts and attachments.
- Updates PHPStan stubs to model the new dynamic
onesearch_thumbnailproperty.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
phpstan/stubs/wordpress-extended.php |
Adds PHPStan typing for the new WP_Post::$onesearch_thumbnail dynamic property. |
inc/Modules/Search/Search.php |
Adds filters + helper logic to render remote thumbnails correctly for search results containing remote/negative-ID posts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ( ! empty( $thumbnail['url'] ) ) { | ||
| $block_content = (string) preg_replace( | ||
| '#(<img[^>]*\bsrc)="[^"]*"([^>]*>)#si', | ||
| '$1="' . esc_url( $thumbnail['url'] ) . '"$2', | ||
| $block_content, | ||
| 1 | ||
| ); | ||
| $block_content = (string) preg_replace( | ||
| '#\s*(?:srcset|sizes)="[^"]*"#', | ||
| '', | ||
| $block_content | ||
| ); | ||
| } |
| $content = (string) preg_replace( | ||
| '#<p\s+class=["\']attachment["\']\s*>.*?</p>#s', | ||
| $attachment_html, | ||
| $content, | ||
| 1 | ||
| ); |
| if ( is_string( $attr ) && ! empty( $attr ) ) { | ||
| $attr_string = ' ' . ltrim( $attr ); | ||
| } |
| if ( is_array( $attr ) ) { | ||
| foreach ( $attr as $name => $value ) { | ||
| if ( 'class' === $name ) { | ||
| $attr_string .= sprintf( ' class="%s"', esc_attr( $value ) ); | ||
| } else { | ||
| $attr_string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) ); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Ignore the copilot noise for now: this is what I was concerned about:
(Left is the local post, right is the remote)
It's possible that the preg_replace can be fixed to better support other block image attachments, and even switching to the more ideal HTML API could catch even more. But ultimately the use of replacing the image output is always going to be flawed, fragile, and susceptible to edge cases.
As mentioned both in the GH issue and on slack, unless you have a reason otherwise, I think the best way to address the bug will be to:
- Programmatically create a single fictitious attachment at plugin activation (and checking/recreating it when using) and assign that number as the post attachment id instead of a non-existent negative post id. It should be hidden from the UX.
- Use the attachment hooks to swap out the metadata on the attachment to the specific remote file, instead of trying to finagle the post directly.
This way any attachment-related functionality work natively the image routing is an isolated implementation detail.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #243 +/- ##
============================================
- Coverage 60.91% 57.85% -3.06%
- Complexity 543 592 +49
============================================
Files 22 22
Lines 1988 2093 +105
============================================
Hits 1211 1211
- Misses 777 882 +105
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
@justlevine, the issue with the current fix isn't clear to me. Could you please help me if I'm missing any details or edge cases? All I can see is a missing publish date and a bit smaller thumbnail size. |
Inspect the actual html for both. Different blocks (and settings) render images differently with different html attributes and patterns. Your search-replace ditches all of that for a specific |
|
Here's the dummy post from above that you can use for testing. Don't forget to update the images after you paste it in. <!-- wp:paragraph -->
<p>image</p>
<!-- /wp:paragraph -->
<!-- wp:image {"id":7,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="http://localhost:8888/wp-content/uploads/2026/06/900-536x354-1.jpg" alt="Some picsum for my testsum" class="wp-image-7"/><figcaption class="wp-element-caption">Picsum ID: 900</figcaption></figure>
<!-- /wp:image -->
<!-- wp:paragraph -->
<p>Gallery</p>
<!-- /wp:paragraph -->
<!-- wp:gallery {"linkTo":"none"} -->
<figure class="wp-block-gallery has-nested-images columns-default is-cropped"><!-- wp:image {"id":7,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="http://localhost:8888/wp-content/uploads/2026/06/900-536x354-1.jpg" alt="Some picsum for my testsum" class="wp-image-7"/><figcaption class="wp-element-caption">Picsum ID: 900</figcaption></figure>
<!-- /wp:image --></figure>
<!-- /wp:gallery -->
<!-- wp:paragraph -->
<p>Media/Text</p>
<!-- /wp:paragraph -->
<!-- wp:media-text {"mediaId":7,"mediaLink":"http://localhost:8888/picsum-id-900/","linkDestination":"none","mediaType":"image"} -->
<div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost:8888/wp-content/uploads/2026/06/900-536x354-1.jpg" alt="Some picsum for my testsum" class="wp-image-7 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} -->
<p>Media text</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->
<!-- wp:cover {"url":"http://localhost:8888/wp-content/uploads/2026/06/900-536x354-1.jpg","id":7,"alt":"Some picsum for my testsum","dimRatio":50,"customOverlayColor":"#9b7861","isUserOverlayColor":false,"isDark":false,"sizeSlug":"full","layout":{"type":"constrained"}} -->
<div class="wp-block-cover is-light"><img class="wp-block-cover__image-background wp-image-7 size-full" alt="Some picsum for my testsum" src="http://localhost:8888/wp-content/uploads/2026/06/900-536x354-1.jpg" data-object-fit="cover"/><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#9b7861"></span><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"placeholder":"Write title…","style":{"typography":{"textAlign":"center"}},"fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Cover Image</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
<!-- wp:post-featured-image /--> |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>


What
Remote site post thumbnail issue on the search area.
Why
It's impacting search result functionality.
Related Issue(s):
Closes https://github.com/rtCamp/OnePress/issues/73
How
The thumbnails were not loading in the search results because remote posts were assigned negative post IDs. To address this, we intercepted those posts and generated the thumbnail data through filters, ensuring that thumbnails are displayed correctly in the search results.
AI Disclosure
Claude Opus 4.6 ( Research )
Testing Instructions
wp-env.Screenshots
Before:

After:

Additional Info
Checklist