Generate all old PDF preview images in WordPress

/ Published in: PHP
Save to your folder(s)

From WordPress 4.7 when you upload a PDF it generates jpg previews. All pdf will remain without previews, or you can use this script to generate previews.

Copy this code and paste it in your HTML
  1. include 'wp-load.php';
  2. include 'wp-admin/includes/image.php';
  4. if ( !current_user_can( 'edit_others_pages' )) die("Not authorized.");
  5. $attachments = $wpdb->get_results("select * from wp_posts where post_mime_type='application/pdf'");
  6. if ($attachments) {
  7. foreach ($attachments as $attachment) {
  8. $filename = str_replace("http://".$_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'], $attachment->guid);
  9. echo $filename."<br/>\n";
  10. $metadata = wp_generate_attachment_metadata($attachment->ID, $filename);
  11. wp_update_attachment_metadata($attachment->ID, $metadata);
  12. }
  13. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.