uploads - How do I display a PDF thumbnail as a link to the PDF without uploading the image

admin2025-01-07  5

When I found a couple of years ago that WordPress will create thumbnails of uploaded PDFs, I was so happy, thinking I wouldn't have to make and upload separate PDF cover images to have a thumbnail display on my site as the link to a PDF. After some struggle I found that the images created from the uploaded PDFs aren't accessible for display on any public webpage. It seems logical to me that since these files are created when I upload a PDF, I should be able to access them, but this isn't possible. I want to clarify that I don't what to embed the PDF (some are very large), I just want the automatically-generated thumbnail to the be the link to the PDF.

I tried the plugin PDF Thumbnails, but it's clunky at best and it doesn't use the automatically-generated images; it has to create its own.

This seems like such a simple and obvious thing to do so I'm really surprised that this isn't possible "out-of-the-box". If I see the thumbnail in admin, why can't I see it on the live site, too?

The post Force documents to appear in Featured Image dialogue doesn't help me because the Twenty Twenty-three theme doesn't have the typical functions.php file and post-type template files, at least that I can find. I should also note that I am using multisite.

Thanks.

When I found a couple of years ago that WordPress will create thumbnails of uploaded PDFs, I was so happy, thinking I wouldn't have to make and upload separate PDF cover images to have a thumbnail display on my site as the link to a PDF. After some struggle I found that the images created from the uploaded PDFs aren't accessible for display on any public webpage. It seems logical to me that since these files are created when I upload a PDF, I should be able to access them, but this isn't possible. I want to clarify that I don't what to embed the PDF (some are very large), I just want the automatically-generated thumbnail to the be the link to the PDF.

I tried the plugin PDF Thumbnails, but it's clunky at best and it doesn't use the automatically-generated images; it has to create its own.

This seems like such a simple and obvious thing to do so I'm really surprised that this isn't possible "out-of-the-box". If I see the thumbnail in admin, why can't I see it on the live site, too?

The post Force documents to appear in Featured Image dialogue doesn't help me because the Twenty Twenty-three theme doesn't have the typical functions.php file and post-type template files, at least that I can find. I should also note that I am using multisite.

Thanks.

Share Improve this question asked Oct 16, 2023 at 18:01 bkeplbkepl 1312 bronze badges 4
  • 1 Untested idea: maybe hook into the upload process from a plugin and create an attachment from the pdf image? Would need custom programming though. – birgire Commented Oct 19, 2023 at 22:44
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Bot Commented Oct 26, 2023 at 13:29
  • Where are your PDF thumbnails generated? If you can't see them, how do you know you have them? – Adam Commented Mar 7, 2024 at 20:45
  • Images are stored within the PDF Object media_details - example $value -> media_details -> sizes -> full -> source_url – dj.cowan Commented Mar 16, 2024 at 0:21
Add a comment  | 

1 Answer 1

Reset to default 0

The PDF images created by wordpress are stored within the PDF attachment object metadata.

[
    {
        "id": 121,
        "media_type": "file",
        "mime_type": "application/pdf",
        ...
        "media_details": {
            "sizes": {
                "full": {
                    ...
                    "source_url": "<domain>/wp-content/uploads/document-name-pdf.jpg"
                }
    }
]

Extracting the image from the PDF

Adding an image block or adding a featured image to a post requires an attachment ID and not simply a url probably

An attachment ID can be created by "side loadeding" the url into the media library to create an attachement using the media_sideload_image function which can return the newly created attachment ID

  • Wordpress media_sideload_image function

Example

$pdf_image_url = $PDF_object -> media_details -> sizes -> full -> source_url;
$attachment_id   = media_sideload_image( 
    $pdf_image_url,
    $post_id, // the post to attach the image 
    'My image title',
    $return_type     = 'id' 
);
$image = set_post_thumbnail( $post_id, $attachment_id );

Set Post Thumbnail

The function set_post_thumbnail( $post_id, $attachment_id ) can be used to assign an image to a post as the featured image

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263961a930.html

最新回复(0)