WordPress禁止生成缩略图

追格官方小助手/ 2022年03月11日/ WordPress/ 浏览 2073

WordPress默认上传图片会生成多个版本的缩略图,久而久之给服务器带来很多不必要的资源,建议禁用WordPress默认生成缩略图功能。


禁用方法

只需将下面代码放到当前WordPress主题functions.php文件内即可:


// 禁用自动生成的图片尺寸
function shapeSpace_disable_image_sizes($sizes) {
    
    unset($sizes['thumbnail']);    // disable thumbnail size
    unset($sizes['medium']);       // disable medium size
    unset($sizes['large']);        // disable large size
    unset($sizes['medium_large']); // disable medium-large size
    unset($sizes['1536x1536']);    // disable 2x medium-large size
    unset($sizes['2048x2048']);    // disable 2x large size
    
    return $sizes;
    
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
 
// 禁用缩放尺寸
add_filter('big_image_size_threshold', '__return_false');
 
// 禁用其他图片尺寸
function shapeSpace_disable_other_image_sizes() {
    
    remove_image_size('post-thumbnail'); // disable images added via set_post_thumbnail_size() 
    remove_image_size('another-size');   // disable any other added image sizes
    
}
add_action('init', 'shapeSpace_disable_other_image_sizes');


发表评论

暂无评论,抢个沙发...

客服 工单