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”
Then this code should be somewhat different. I’ll try it out tomorrow or this weekend.
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?
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.
@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.
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.
@erolsk8 Thanks so much for your help with this, I can confirm that code allows us to ignore “-pdf.jpg” files as well.
There’s been a change to the media_sync_filter_is_scan_object_ignored filter, so the new examples should look like this:
/**
* Overwrite Media Sync plugin rules for skipping scanned files or folders.
*
* Should return bool value.
*
* @param boolean $is_ignored Default rules for this file/folder (skipping thumbnails, index.php, hidden files, etc.)
* @param string $full_path Each scanned file/folder absolute path
* @param string $file_name Each scanned file/folder name
* @return boolean
*/
function my_custom_media_sync_additional_file_skip($is_ignored, $full_path, $file_name)
{
// Get path info
$file_path_parts = pathinfo(wp_normalize_path($full_path));
// 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);
/**
* Overwrite Media Sync plugin rules for skipping scanned files or folders.
*
* Should return bool value.
*
* @param boolean $is_ignored Default rules for this file/folder (skipping thumbnails, index.php, hidden files, etc.)
* @param string $full_path Each scanned file/folder absolute path
* @param string $file_name Each scanned file/folder name
* @return boolean
*/
function my_custom_media_sync_additional_file_skip($is_ignored, $full_path, $file_name)
{
$is_this_pdf_thumbnail = strpos($file_name, '-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);
Should have updated this topic before that change, and these filters should be documented somewhere.