Generate all old PDF preview images with WordPress 4.7

WordPress 4.7 generates thumbnails for PDF files while uploading them in the media library, this is a really good thing…

December 13, 2016

WordPress 4.7 generates thumbnails for PDF files while uploading them in the media library, this is a really good thing especially if you have a site that uses PDF to publish some kind of contents (books, or lessons, documents or anything else) this can be really useful.

Even if, probably, you’ve already used┬áthe featured image to upload the first page of the PDF or a cover file.

To automatically create the images of all the PDF in your library, you have to trigger the file generation functions for all the file that you already have in your media library.

Here is a small script to process all your PDF files to generate thumbnails, there are two methods to call for each image:

1) wp_generate_attachment_metadata to generate the thumbnails
2) wp_update_attachment_metadata to store the new metadata generated in the database.

After running this script you will see the images in the media library an you can use the WordPress functions to show the attachments in your theme or plugin.

Place this file in the root. Run once and then remove it.

include 'wp-load.php';
include 'wp-admin/includes/image.php';
if ( !current_user_can( 'edit_others_pages' )) die("Not authorized.");
$attachments = $wpdb->get_results("select * from wp_posts where post_mime_type='application/pdf'");
if ($attachments) {
	foreach ($attachments as $attachment) {
		$filename = str_replace("http://".$_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'], $attachment->guid);
		echo $filename."<br/>\n";
		$metadata = wp_generate_attachment_metadata($attachment->ID, $filename);
		wp_update_attachment_metadata($attachment->ID, $metadata);


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.


WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache

WordPress Gutenberg doesn’t parse Istagram oembed url

Sometimes Instagram embed doesn't work, why?

November 4, 2019

Stop sending email to new user in WordPress admin registration

A code snippet to prevent WordPress to send notification emails when you manually create new users. It uses a pluggable function.

November 1, 2019