When PDF preview images are available, how does the Media Library determine that it needs to display them? Does it check for a '_wp_attachment_metadata' record in the postmeta table? And where in Wordpress is the code that does this?


The preview images for PDF documents are created within wp_generate_attachment_metadata() [src].

The full size preview image will have the -pdf.jpg extension [src].

The preview images info for the PDF document is stored in _wp_attachment_metadata in the post meta table.

Here's an example data for a test.pdf document:

    [0] => Array
            [sizes] => Array
                    [thumbnail] => Array
                            [file] => test-pdf-116x150.jpg
                            [width] => 116
                            [height] => 150
                            [mime-type] => image/jpeg

                    [full] => Array
                            [file] => test-pdf.jpg
                            [width] => 1088
                            [height] => 1408
                            [mime-type] => image/jpeg



This is stored as a serialized array under the _wp_attachment_metadata key in the post meta table, but we display it here as an array for better readability.

It's fetched with wp_get_attachment_metadata() [src], as for any other image attachment.

We can see it e.g. in wp_prepare_attachment_for_js() [src].

Also in image_downsize()[src], that's used by wp_get_attachment_image_src() [src].

The latter one is used by wp_get_attachment_image(), that's called within the column_title() method of the media list table, to display the image preview [src].

Hope it helps!

  • Thus to automatically get the PDF preview image, one must use wp_get_attachment_image() instead of wp_get_attachment_thumb_url(). – AlanP Nov 17 '17 at 10:59
  • Both should work to fetch the pdf preview image, as both use wp_get_attachment_metadata() implicitly. Just depends if you want HTML output or an url output. @AlanP – birgire Nov 17 '17 at 12:39
  • Yes, wp_get_attachment_thumb_url() does work now. – AlanP Nov 17 '17 at 12:50
  • Great - I'm not sure if a preview image can be generated from every uploaded pdf document, e.g. from corrupted ones. So one should in general have a fallback @AlanP – birgire Nov 17 '17 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.