Protect PDF Thumbnail Images with PDA Gold

Before version 3.1.2, PDF thumbnails are not protected by our PDA Gold. So even when you set the File Access Permission to everyone, the PDF thumbnails will not display.

At the moment, we’ve already checked and restricted access to all files under our protected folder including those PDF thumbnails. However, by rights those PDF thumbnails should follow the PDF’s permission.

Since complex checking leads to slower performance, i.e. image loading, we don’t include that into our Gold features yet. That’s why those thumbnails are not accessible.

In order for the PDF thumbnails to follow the File Access Permission, simply put this code under your child theme function.php

add_filter( 'pda_handle_attachment_id', 'pda_get_attachment_id', 10, 3 );

function pda_get_attachment_id( $attachment_id, $file_path, $mime ) {
   if ( $attachment_id ) {
      return $attachment_id;
   }

   // Only allow image file type.
   if ( false === $mime['type'] || false === strpos( $mime['type'], 'image' ) ) {
      return $attachment_id;
   }

   global $wpdb;
   $basename = wp_basename( $file_path );

   $query    = $wpdb->prepare(
      "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value LIKE %s",
      '_wp_attachment_metadata',
      "%$basename%"
   );
   $metadata = $wpdb->get_row( $query );

   if ( empty( $metadata ) ) {
      return $attachment_id;
   }

   return (int) $metadata->post_id;
}

Note: This code only works for PDA Gold from version 3.2.0

Now all the PDF thumbnails will be accessible to anyone with the access permission.

Lasted updated on May 19, 2020