Skip to content

Instantly share code, notes, and snippets.

@nosilver4u
Last active November 10, 2022 02:43
Show Gist options
  • Save nosilver4u/eb858df10521aece2044a3a15ccdd17b to your computer and use it in GitHub Desktop.
Save nosilver4u/eb858df10521aece2044a3a15ccdd17b to your computer and use it in GitHub Desktop.
Force Regenerate Thumbnails Tweaks
<?php
/*
Plugin Name: Force Regenerate Thumbnails Tweaks
Version: 1.0.0
*/
// NOTE: This plugin will do nothing without alterations. Uncomment the add_filter() lines for the filters you wish to use.
// If necessary, edit them to your needs. Some filters simply turn things on/off by returning a true or false.
// For such filters, using WordPress' built-in __return_true() or __return_false() functions will work just fine, unless
// you wish to conditionally enable/disable a feature.
// 1. In case you ever need to force WP and FRT to use the GD image library instead of imagick (ImageMagick).
// Probably not very common anymore, but once upon a time that was helpful to resolve thumbnail generation failures.
// add_filter( 'regenerate_thumbs_force_gd', '__return_true' );
// 2. Change the permissions required to regenerate thumbnails. Defaults to 'manage_options'.
// add_filter( 'regenerate_thumbs_cap', 'my_frt_permissions' );
function my_frt_permissions( $capability ) {
return 'edit_others_posts';
}
// 3. Skip regeneration for a particular attachment. This keeps the default priority of 10, and specifies that the filter
// function accepts 2 parameters: $skip, and $attachment_id.
// add_filter( 'regenerate_thumbs_skip_image', 'my_frt_skip_image', 10, 2 );
function my_frt_skip_image( $skip, $attachment_id ) {
if ( 13 === (int) $attachment_id ) {
return true;
}
return $skip;
}
// 4. "Weak mode" does not delete any thumbs, and effectively makes FRT behave like any other thumbnail regen plugin.
// AFAIK, WordPress will still overwrite existing thumbnails by default, and there is no way to avoid this.
// This one just enables "Weak mode" for all images.
// add_filter( 'regenerate_thumbs_weak', '__return_true' );
// Or you can use the second parameter, which allows you to prevent specific thumbs from being deleted.
// add_filter( 'regenerate_thumbs_weak', 'my_frt_weak_regen', 10, 2 );
function my_frt_weak_regen( $weak_mode, $thumb_fullpath ) {
if ( false !== strpos( $thumb_fullpath, 'thumbs_i_want_to_skip' ) ) {
return true;
}
return $weak_mode;
}
// 5. By default, FRT uses the original unscaled image to generate thumbs (if one exists), instead of the 2560 px full-size
// image that may have been created by WordPress during upload of hi-res images. This filter makes FRT use the full-size
// instead of the pre-scaled original.
// add_filter( 'regenerate_thumbs_original_image', '__return_false' );
// This filter can also be used to modify which image is used as the original for thumbnail generation, or to conditionally
// skip use of the originals.
// add_filter( 'regenerate_thumbs_original_image', 'my_frt_original_image' );
function my_frt_original_image( $original_path ) {
if ( false !== strpos( $original_path, 'bad_original' ) ) {
return false;
}
return $original_path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment