Created
September 17, 2023 17:21
-
-
Save nkryptic/90279bc7c8d5e2eb4eebb091a889e73c to your computer and use it in GitHub Desktop.
functions/hook to remove physical files of Wordpress subimages that PressThumb will generate dynamically (Pagely specific)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note: this code is specific to Wordpress running on Pagely with the PressThumb | |
service enabled. | |
Adding this code to your theme's functions.php file will allow uploaded media | |
to generate subimages and the metadata associated with them, but then remove | |
the physical files generated for those subimages (aka intermediate image sizes). | |
By leaving the metadata, this allows the rest of Wordpress and it's plugins to | |
continue referencing them as they'd expect. So, you can simply leave the custom | |
image sizes in Wordpress (and theme/plugins) as they are and your disk usage | |
won't be overblown. | |
After the code is added, newly uploaded media's subimage assets will be removed. | |
If you have existing uploaded media and want to remove the subimage assets, you | |
can do so through a wp-cli shell: | |
$ wp shell | |
wp> pressthumb__remove_files_of_subimages_for_all_attachments() | |
wp> exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove the phsyical files created for the intermediate image sizes but | |
* after they're created to ensure the metadata is generated and saved for | |
* the attachment. The PressThumb service dynamically creates these images | |
* outside of Wordpress, so we can save disk space by removing them. | |
* | |
* Note: will interfere with any thumbnail regeration attempts | |
*/ | |
add_filter( 'wp_generate_attachment_metadata', 'pressthumb__remove_files_of_attachment_subimages', 99, 3 ); | |
function pressthumb__remove_files_of_attachment_subimages( $metadata, $attachment_id, $context ) { | |
$pressthumb_pattern = '![^/]+\-[1-9][0-9]+x[1-9][0-9]+\.(jpe?g|png)$!'; | |
$attachment_path = get_attached_file( $attachment_id ); | |
$base_dir = dirname( $attachment_path ); | |
if ( !empty( $metadata['sizes'] ) ) { | |
foreach( $metadata['sizes'] as $size => $image ) { | |
// PDF attachments have a preview image in the array under the 'full' key. | |
// The other intermediate images are all based on it, so we need to skip it. | |
if ( 'full' === $size ) { | |
continue; | |
} | |
// images scaled in wordpress keep originals around in the postmata key _wp_attachment_backup_sizes, | |
// but we should keep the one with the 'original-full' key. we only see this when this | |
// function is called by pressthumb__remove_files_of_subimages_for_all_attachments | |
if ( 'full-orig' === $size ) { | |
continue; | |
} | |
// only remove files that match the pattern for PressThumb to generate them on the fly | |
if ( !empty( $image['file'] ) && preg_match( $pressthumb_pattern, $image['file'] ) ) { | |
$file_path = path_join( $base_dir, $image['file'] ); | |
if ( file_exists( $file_path ) ) { | |
@unlink( $file_path ); | |
} | |
} | |
} | |
} | |
return $metadata; | |
} | |
/** | |
* Removes physical files of image versions that PressThumb generates | |
* outside of Wordpress | |
*/ | |
function pressthumb__remove_files_of_subimages_for_all_attachments() { | |
$args = array( | |
'post_type' => 'attachment', | |
'post_mime_type__in' => array( | |
'application/pdf', | |
'image/jpeg', | |
'image/png', | |
), | |
'fields' => 'ids', | |
'posts_per_page' => -1, | |
); | |
$attachment_ids = get_posts( $args ); | |
foreach ( $attachment_ids as $attachment_id ) { | |
$metadata = wp_get_attachment_metadata( $attachment_id ); | |
pressthumb__remove_files_of_attachment_subimages( $metadata, $attachment_id, null ); | |
// check for backup images too | |
$metadata = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); | |
if ( !empty( $metadata ) ) { | |
pressthumb__remove_files_of_attachment_subimages( array( 'sizes' => $metadata ), $attachment_id, null ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment