Support » Plugin: Media Sync » PDF Thumbnails not excluded from sync

  • Resolved van8uren

    (@van8uren)


    Thanks for the awesome plugin, its very useful!

    We use mediasync to add PDF documents to our media library.

    Intial sync goes fine, and the PDF files are added to the media library correctly.

    When the PDFs are added to the media library, WordPress creates a thumbnail of the PDF. e.g. file.pdf has a file.jpg thumbnail created for it, as well as different sized thumbnails such as file-pdf-106×150.jpg.

    If you then sync the media library again, the plugin correctly ignores file-pdf-106×150.jpg, however it does not ignore file.jpg, even though it is thumbnail of file.pdf and shouldnt be added to the media library by itself.

    Would it be possible to have plugin ignore the thumbnails created for PDF files?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author erolsk8

    (@erolsk8)

    Oh well, that’s interesting.

    Currently, there is no configuration for that. But I think it should be added and it should be optional. Because it can make everything slower, which is already a problem when someone has thousands of files.

    Until that is available, you could try adding something like this (to functions.php of your template for example):

    
    /**
     * Overwrite Media Sync plugin rules for skipping scanned files or folders.
     *
     * File/folder contains name and absolute path:
     * $file->getFilename()
     * $file->getPathname()
     *
     * Should return bool value.
     *
     * @param boolean $is_ignored Default rules for this file/folder (skipping thumbnails, index.php, hidden files, etc.)
     * @param RecursiveDirectoryIterator $file Each scanned file/folder
     * @return boolean
     */
    function my_custom_media_sync_additional_file_skip($is_ignored, $file)
    {
        // Get path info
        $file_path_parts = pathinfo(wp_normalize_path($file->getPathname()));
    
        // Get each file without extension
        $file_without_extension = $file_path_parts['dirname'] . '/'. $file_path_parts['filename'];
    
        // If .pdf file pair exists for this file, we'll consider this file to be a PDF thumbnail
        $is_this_pdf_thumbnail = file_exists($file_without_extension . '.pdf');
    
        return $is_ignored || $is_this_pdf_thumbnail;
    }
    add_filter('media_sync_filter_is_scan_object_ignored', 'my_custom_media_sync_additional_file_skip', 10, 3);
    

    I mean this is just a rough idea, I didn’t test this.

    Thanks for the code, we will try that out.

    Apologies, I just noticed I was wrong the in my initial post when I said “e.g. file.pdf has a file.jpg thumbnail created for it”

    The thumbnail created by WordPress actually ends in -pdf.jpg.

    So I should have said “e.g. file.pdf has a file-pdf.jpg thumbnail created for it”

    Plugin Author erolsk8

    (@erolsk8)

    Then this code should be somewhat different. I’ll try it out tomorrow or this weekend.

    Plugin Author erolsk8

    (@erolsk8)

    Hey @van8uren, how can I make Media Library to generate those thumbnails? I tried uploading some PDFs but I don’t get those thumbnails. Do you maybe use some plugin that does that?

    Plugin Author erolsk8

    (@erolsk8)

    But if you want to ignore all files containing -pdf.jpg, you can try with this:

    
    /**
     * Overwrite Media Sync plugin rules for skipping scanned files or folders.
     *
     * File/folder contains name and absolute path:
     * $file->getFilename()
     * $file->getPathname()
     *
     * Should return bool value.
     *
     * @param boolean $is_ignored Default rules for this file/folder (skipping thumbnails, index.php, hidden files, etc.)
     * @param RecursiveDirectoryIterator $file Each scanned file/folder
     * @return boolean
     */
    function my_custom_media_sync_additional_file_skip($is_ignored, $file)
    {
        $is_this_pdf_thumbnail = strpos($file->getFilename(), '-pdf.jpg') !== false;
    
        return $is_ignored || $is_this_pdf_thumbnail;
    }
    add_filter('media_sync_filter_is_scan_object_ignored', 'my_custom_media_sync_additional_file_skip', 10, 3);
    

    I tried it with manually created “-pdf.jpg” files and it seems to work.

    van8uren

    (@van8uren)

    @erolsk8 Hmmm as far as I know it is a core function of WordPress, it seems to have been added in 4.7, I dont think any of the plugins we use would specifically enable it. The only one I can think of is possible WooCommerce?

    Thank you for the updated code snippet, we will give that a try.

    Plugin Author erolsk8

    (@erolsk8)

    Hmm strange, I’m trying with WP 5.4.1 with various PDFs and I thought it might happen only when adding PDF to a Post. So I tried that and still nothing.

    But in any case, I created those -pdf.jpg files manually, and that hook function I sent should ignore those files. And then if you want, you can add additional logic there.

    van8uren

    (@van8uren)

    @erolsk8 Thanks so much for your help with this, I can confirm that code allows us to ignore “-pdf.jpg” files as well.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.