Since 4.7, Wordpress generates images representing the first page of any PDF that is uploaded. https://make.wordpress.org/core/2016/11/15/enhanced-pdf-support-4-7/

This is useful when choosng what to link to and I know that you can use these images in a theme with wp_get_attachment_image.

My question is, can you inset these images in the content area of a page/post using the Add Media button?


There's no easy way in version 4.8, as far as I know.

Few ideas come to mind, regarding writing plugins to make it easier:

  • Use the media_send_to_editor filter to modify the HTML output for the application/pdf mime type, to include the preview image. This makes it is easier for user to insert pdf preview images, but hardcodes the HTML. Also note that if the pdf attachment is later deleted, the preview images are deleted too. This could resulted in broken pdf preview images on the front-end, but that's nothing new, as we've seen the same happen for image attachments, that we later delete.

  • Same as above but return a custom shortcode instead of HTML. This is not as user friendly but makes it easier to adjust the HTML later on and make sure the pdf isn't deleted, to avoid broken images.

  • Generate a shortcode string for each pdf attachment that the user can just copy/paste into the editor. Also less user friendly, but flexible.

  • Create a real image attachment for the pdf preview images, so it would be stored in the wp_posts table and have it's own separated upload files. Filters like fallback_intermediate_image_sizes and wp_generate_attachment_metadata would be helpful entry points. This would be more complex to write and would most likely not solve all the issues related to a good user experience.

Note that the info on the pdf preview images is e.g. stored in the _wp_attachment_metadata meta value and we can see here in the core how they are generated with the WordPress Image Editor API.

Hope it helps!

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.