在主题functions.php函数文件里添加即可,图片只有jpg格式适合压缩。
function wt_handle_upload_callback($data) { $image_quality = 60; // 压缩比例 0-100选择 $file_path = $data['file']; $image = false; switch ( $data['type'] ) { case 'image/jpeg': { $image = imagecreatefromjpeg( $file_path ); imagejpeg( $image, $file_path, $image_quality ); break; } case 'image/png': { //$image = imagecreatefrompng( $file_path ); //imagepng( $image, $file_path, $image_quality); //png不支持压缩,可以使用webp代替,但是ie浏览器不支持webp文件格式 //imagewebp( $image, $file_path, $image_quality); break; } case 'image/gif': { // Nothing to do here since imagegif doesn't have an 'image quality' option break; } } return $data; } add_filter('wp_handle_upload', 'wt_handle_upload_callback');