To display external images as thumbnails for virtual posts without uploading them to the WordPress media library, you can leverage WordPress filters and hooks to manipulate how the thumbnail is displayed. The issue stems from how WordPress looks for image attachments in its own database. Since the external images aren’t stored locally, you need to inject them dynamically into the theme’s template.
Here’s how you can address this problem:
1. Override Thumbnail Display for Virtual Posts
You’re on the right track with using the post_thumbnail_html
filter, but you might need a more refined approach to ensure the external image URL is injected properly.
Updated Solution Using post_thumbnail_html
Filter
In your plugin, use the following code to dynamically inject the external image into the thumbnail slot without needing to upload it:
php
Copy code
add_filter('post_thumbnail_html', function ($html, $post_id, $post_thumbnail_id, $size, $attr) {
global $post;
// Check if this is a virtual post and if it has an external image URL
if (isset($post->is_virtual) && $post->is_virtual && !empty($post->primaryImage)) {
// Return an <img> tag with the external image URL
$html = '<img src="' . esc_url($post->primaryImage) . '" class="wp-post-image" />';
}
return $html;
}, 10, 5);
2. Modify the Post Data
Ensure that your virtual post object contains a property like primaryImage
that holds the URL of the external image. You already seem to have this part in place, but ensure that your virtual posts look something like this:
php
Copy code
$post = (object) [
'ID' => 0, // Virtual post ID (no actual ID)
'post_title' => 'Example Title',
'post_excerpt' => 'This is a virtual post excerpt.',
'post_date' => current_time('mysql'),
'is_virtual' => true, // Flag to identify virtual posts
'primaryImage' => 'https://external-site.com/image.jpg', // External image URL
];
3. Use the has_post_thumbnail()
Filter
Since WordPress checks for thumbnails using has_post_thumbnail()
, you should override this behavior for virtual posts by returning true
if the post is virtual and has an external image URL:
php
Copy code
add_filter('has_post_thumbnail', function ($has_thumbnail, $post) {
// Check if the post is virtual and has a primary image URL
if (isset($post->is_virtual) && $post->is_virtual && !empty($post->primaryImage)) {
return true; // Indicate that this virtual post has a thumbnail
}
return $has_thumbnail;
}, 10, 2);
4. Ensure External Images Are Displayed in Loops
Now, WordPress should display the external image as the post thumbnail in the post loop. To make sure your theme or front-page template uses the post_thumbnail_html
filter and respects the external image, verify that the theme’s template files (e.g., content.php
, archive.php
) are using the_post_thumbnail()
function correctly.
5. Additional Consideration: Custom Post Class or CSS
If you need to add custom CSS or further manipulate the virtual posts differently from regular posts, you can also add a custom class:
php
Copy code
add_filter('post_class', function ($classes, $class, $post_id) {
global $post;
// Add a custom class for virtual posts
if (isset($post->is_virtual) && $post->is_virtual) {
$classes[] = 'virtual-post';
}
return $classes;
}, 10, 3);